Dates and times in Excel are represented by real numbers. For example a date that is displayed in Excel as "Jan 1 2013 12:00 PM" is stored as the number 41275.5.
The integer part of the number stores the number of days since the epoch, which is generally 1900, and the fractional part stores the percentage of the day.
A date or time in Excel is just like any other number. To display the number as a date you must apply an Excel number format to it. Here is an example:
#include "xlsxwriter.h"
int main() {
double number = 41333.5;
Struct to represent an Excel workbook.
Definition: workbook.h:293
Struct to represent an Excel worksheet.
Definition: worksheet.h:2115
lxw_workbook * workbook_new(const char *filename)
Create a new workbook object.
lxw_format * workbook_add_format(lxw_workbook *workbook)
Create a new Format object to formats cells in worksheets.
lxw_error workbook_close(lxw_workbook *workbook)
Close the Workbook object and write the XLSX file.
lxw_worksheet * workbook_add_worksheet(lxw_workbook *workbook, const char *sheetname)
Add a new worksheet to a workbook.
lxw_error worksheet_set_column(lxw_worksheet *worksheet, lxw_col_t first_col, lxw_col_t last_col, double width, lxw_format *format)
Set the properties for one or more columns of cells.
lxw_error worksheet_write_number(lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, double number, lxw_format *format)
Write a number to a worksheet cell.
}
Some options for creating or converting dates to the correct format are shown below.
Writing datetimes with the lxw_datetime struct
To make working with dates and times a little easier the libxlsxwriter
library provides the lxw_datetime struct and the worksheet_write_datetime()
function.
The members of the lxw_datetime struct and the range of their values are:
Member | Value |
year | 1900 - 9999 |
month | 1 - 12 |
day | 1 - 31 |
hour | 0 - 23 |
min | 0 - 59 |
sec | 0 - 59.999 |
Dates in Excel do not support timezones and the maximum resolution of times is milliseconds.
If dates or times are required without the other you should initialize the unrequired values to 0
:
Struct to represent a date and time in Excel.
Definition: common.h:159
Using lxw_datetime and worksheet_write_datetime() the previous example can then be re-written as follows:
#include "xlsxwriter.h"
int main() {
lxw_error worksheet_write_datetime(lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, lxw_datetime *datetime, lxw_format *format)
Write a date or time to a worksheet cell.
}
The output from this program is the same as the previous example.
Writing Unix datetimes
Another alternative when handling dates is Unix Time which is a common integer time format. It is defined as the number of seconds since the Unix epoch (1970-01-01 00:00 UTC).
The worksheet_write_unixtime()
function can be used to write dates and times in this format. Negative values can also be used for dates prior to 1970:
#include "xlsxwriter.h"
int main() {
}
lxw_error worksheet_write_unixtime(lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, int64_t unixtime, lxw_format *format)
Write a Unix datetime to a worksheet cell.
The output from this program is:
Date formatting
Dates can be formatted using any of the date formats supported by Excel. Here is a longer example that shows the same date in a several different formats:
#include "xlsxwriter.h"
int main() {
uint32_t row = 0;
uint16_t col = 0;
int i;
const char *date_formats[] = {
"dd/mm/yy",
"mm/dd/yy",
"dd m yy",
"d mm yy",
"d mmm yy",
"d mmmm yy",
"d mmmm yyy",
"d mmmm yyyy",
"dd/mm/yy hh:mm",
"dd/mm/yy hh:mm:ss",
"dd/mm/yy hh:mm:ss.000",
"hh:mm",
"hh:mm:ss",
"hh:mm:ss.000",
};
for (i = 0; i < 14; i++) {
row++;
}
lxw_error worksheet_write_string(lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const char *string, lxw_format *format)
Write a string to a worksheet cell.
}
To get date formats that show up in Excel as a "Date" or "Time" number category see Number Format Categories.
Next: Working with Charts