Method: Writeexcel::Worksheet#write_date_time
- Defined in:
- lib/writeexcel/worksheet.rb
#write_date_time(*args) ⇒ Object
:call-seq:
write_date_time(row, col , date_string[, format])
write_date_time(A1_notation, date_string[, format])
Write a datetime string in ISO8601 “yyyy-mm-ddThh:mm:ss.ss” format as a number representing an Excel date. format is optional.
Returns 0 : normal termination
-1 : insufficient number of arguments
-2 : row or column out of range
-3 : Invalid date_time, written as string
The write_date_time() method can be used to write a date or time to the cell specified by row and column:
worksheet.write_date_time('A1', '2004-05-13T23:20', date_format)
The date_string should be in the following format:
yyyy-mm-ddThh:mm:ss.sss
This conforms to an ISO8601 date but it should be noted that the full range of ISO8601 formats are not supported.
The following variations on the date_string parameter are permitted:
yyyy-mm-ddThh:mm:ss.sss # Standard format
yyyy-mm-ddT # No time
Thh:mm:ss.sss # No date
yyyy-mm-ddThh:mm:ss.sssZ # Additional Z (but not time zones)
yyyy-mm-ddThh:mm:ss # No fractional seconds
yyyy-mm-ddThh:mm # No seconds
Note that the T is required in all cases.
A date should always have a format, otherwise it will appear as a number, see “DATES AND TIME IN EXCEL” and “CELL FORMATTING”. Here is a typical example:
date_format = workbook.add_format(:num_format => 'mm/dd/yy')
worksheet.write_date_time('A1', '2004-05-13T23:20', date_format)
Valid dates should be in the range 1900-01-01 to 9999-12-31, for the 1900 epoch and 1904-01-01 to 9999-12-31, for the 1904 epoch. As with Excel, dates outside these ranges will be written as a string.
See also the date_time.rb program in the examples directory of the distro.
3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 |
# File 'lib/writeexcel/worksheet.rb', line 3299 def write_date_time(*args) # Check for a cell reference in A1 notation and substitute row and column args = row_col_notation(args) return -1 if args.size < 3 # Check the number of args row, col, str, format = args # Check that row and col are valid and store max and min values return -2 unless check_dimensions(row, col) == 0 date_time = convert_date_time(str, date_1904?) if date_time error = write_number(row, col, date_time, args[3]) else # The date isn't valid so write it as a string. write_string(row, col, str, format) error = -3 end error end |