如何用C++管理日期和时间
Contents
[
Hide
]
如何在Excel中存储日期和时间
日期和时间在单元格中以数字存储。因此,包含日期和时间的单元格的值是数字类型。指定日期和时间的数字由日期(整数部分)和时间(小数部分)组成。Cell::GetDoubleValue()
方法返回此数字。
如何在Aspose.Cells中显示日期和时间
要将数字显示为日期和时间,请通过 Style::SetNumber()
或 Style::SetCustom()
方法将所需的日期和时间格式应用于单元格。Cell::GetDateTimeValue()
方法返回 DateTime
对象,该对象指定了由单元格中的数字表示的日期和时间。
如何在Aspose.Cells中切换两个日期系统
MS-Excel 将日期存储为称为序列值的数字。 序列值是从日期系统中的第一天经过的天数,它是一个整数。 Excel 支持以下日期系统的序列值:
- 1900 年日期系统。 第一个日期是 1900 年 1 月 1 日,其序列值为 1。 最后一个日期是 9999 年 12 月 31 日,其序列值为 2,958,465。 此日期系统默认用于工作簿。
- 1904年日期系统。第一个日期是1904年1月1日,其序列值为0。最后一个日期是9999年12月31日,其序列值为2,957,003。要在工作簿中使用此日期系统,请设置
Workbook::GetSettings()->SetDate1904(true)
属性。
此示例显示了在不同日期系统中存储的相同日期的序列值不同。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
Workbook workbook;
workbook.GetSettings().SetDate1904(false);
Worksheet ws = workbook.GetWorksheets().Get(0);
Cells cells = ws.GetCells();
Cell a1 = cells.Get(u"A1");
a1.PutValue(45237.0);
if (a1.GetType() == CellValueType::IsNumeric)
{
std::cout << "A1 is Numeric Value: " << a1.GetDoubleValue() << std::endl;
}
workbook.GetSettings().SetDate1904(true);
std::cout << "use The 1904 date system====================" << std::endl;
Cell a2 = cells.Get(u"A2");
a2.PutValue(43775.0);
if (a2.GetType() == CellValueType::IsNumeric)
{
std::cout << "A2 is Numeric Value: " << a2.GetDoubleValue() << std::endl;
}
Aspose::Cells::Cleanup();
return 0;
}
输出结果:
A1 is Numeric Value: 45253
use The 1904 date system====================
A2 is Numeric Value: 43791
如何在 Aspose.Cells 中设置 DateTime 值
此示例在单元格A1和A2中设置 DateTime
值,为A1设置自定义格式,为A2设置数字格式,然后输出值类型。
#include <iostream>
#include <ctime>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
Workbook workbook;
Worksheet ws = workbook.GetWorksheets().Get(0);
Cells cells = ws.GetCells();
Cell a1 = cells.Get(u"A1");
time_t now = time(nullptr);
double oaDate1 = static_cast<double>(now) / (60 * 60 * 24) + 25569.0;
a1.PutValue(oaDate1);
if (a1.GetType() == CellValueType::IsNumeric)
{
std::cout << "A1 is Numeric Value: " << a1.IsNumericValue() << std::endl;
}
Style a1Style = a1.GetStyle();
a1Style.SetCustom(u"mm-dd-yy hh:mm:ss", true);
a1.SetStyle(a1Style);
if (a1.GetType() == CellValueType::IsDateTime)
{
std::cout << "Cell A1 contains a DateTime value." << std::endl;
}
else
{
std::cout << "Cell A1 does not contain a DateTime value." << std::endl;
}
Cell a2 = cells.Get(u"A2");
now = time(nullptr);
double oaDate2 = static_cast<double>(now) / (60 * 60 * 24) + 25569.0;
a2.SetValue(oaDate2);
if (a2.GetType() == CellValueType::IsNumeric)
{
std::cout << "A2 is Numeric Value: " << a2.IsNumericValue() << std::endl;
}
Style a2Style = a2.GetStyle();
a2Style.SetNumber(22);
a2.SetStyle(a2Style);
if (a2.GetType() == CellValueType::IsDateTime)
{
std::cout << "Cell A2 contains a DateTime value." << std::endl;
}
else
{
std::cout << "Cell A2 does not contain a DateTime value." << std::endl;
}
Aspose::Cells::Cleanup();
}
输出结果:
A1 is Numeric Value: True
Cell A1 contains a DateTime value.
A2 is Numeric Value: True
Cell A2 contains a DateTime value.
如何在 Aspose.Cells 中获取 DateTime 值
此示例在单元格A1和A2中设置 DateTime
值,为A1设置自定义格式,为A2设置数字格式,检查两个单元格的值类型,然后输出 DateTime
值和格式化字符串。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main() {
Aspose::Cells::Startup();
Workbook workbook;
Worksheet ws = workbook.GetWorksheets().Get(0);
Cells cells = ws.GetCells();
Cell a1 = cells.Get(u"A1");
a1.PutValue(Date{2023, 5, 15});
if (a1.GetType() == CellValueType::IsNumeric) {
std::cout << "A1 is Numeric Value: " << a1.IsNumericValue() << std::endl;
}
Style a1Style = a1.GetStyle();
a1Style.SetCustom(u"mm-dd-yy hh:mm:ss", true);
a1.SetStyle(a1Style);
if (a1.GetType() == CellValueType::IsDateTime) {
std::cout << "Cell A1 contains a DateTime value." << std::endl;
Date dateTimeValue = a1.GetDateTimeValue();
std::cout << "A1 DateTime String Value: " << a1.GetStringValue().ToUtf8() << std::endl;
}
else {
std::cout << "Cell A1 does not contain a DateTime value." << std::endl;
}
Cell a2 = cells.Get(u"A2");
a2.PutValue(Date{2023, 5, 16});
if (a2.GetType() == CellValueType::IsNumeric) {
std::cout << "A2 is Numeric Value: " << a2.IsNumericValue() << std::endl;
}
Style a2Style = a2.GetStyle();
a2Style.SetNumber(22);
a2.SetStyle(a2Style);
if (a2.GetType() == CellValueType::IsDateTime) {
std::cout << "Cell A2 contains a DateTime value." << std::endl;
Date dateTimeValue = a2.GetDateTimeValue();
std::cout << "A2 DateTime String Value: " << a2.GetStringValue().ToUtf8() << std::endl;
}
else {
std::cout << "Cell A2 does not contain a DateTime value." << std::endl;
}
Aspose::Cells::Cleanup();
return 0;
}
输出结果:
A1 is Numeric Value: True
Cell A1 contains a DateTime value.
A1 DateTime Value: 11/23/2023 5:59:09 PM
A1 DateTime String Value: 11-23-23 17:59:09
A2 is Numeric Value: True
Cell A2 contains a DateTime value.
A2 DateTime Value: 11/23/2023 5:59:09 PM
A2 DateTime String Value: 11/23/2023 17:59