Examples of Date object and functions
First: let dt = new Date(); // current date/time
dt.toLocaleString():
dt.getMonth(): [0-11, with January==0]
dt.getDate(): [1-31]
dt.getDay(): [0-6, with Sunday==0]
dt.getFullYear():
dt.getHours(): [0-23]
dt.getMinutes(): [0-59]
dt.getSeconds(): [0-59]
Second: dt = new Date("2018-03-09T09:18:00"); // specified state/time
dt.toLocaleString():
dt.getMonth(): [0-11, with January==0]
dt.getDate(): [1-31]
dt.getDay(): [0-6, with Sunday==0]
dt.getFullYear():
dt.getHours(): [0-23]
dt.getMinutes(): [0-59]
dt.getSeconds(): [0-59]
Third: Set fields individually (to January 1, 2003, 12:34:56)
dt.setMonth(0);
dt.setDate(1);
dt.setFullYear(2003);
dt.setHours(12);
dt.setMinutes(34);
dt.setSeconds(56);
dt.toLocaleString():
dt.getMonth(): [0-11, with January==0]
dt.getDate(): [1-31]
dt.getDay(): [0-6, with Sunday==0]
dt.getFullYear():
dt.getHours(): [0-23]
dt.getMinutes(): [0-59]
dt.getSeconds(): [0-59]
Fourth: Using the dataformat.js library
Make sure you include the dateformat.js library using a script element.
Create a Date object. Then you can get back a formatted string based on that
date using the Date's new .format(formatString) function. Anything in the
format string will stay as it is, except for specific two character sequences
starting with a % sign. Here are the recognized sequences:
- %a: short day name (Sun, Mon, ...)
- %A: long day name (Sunday, Monday, ...)
- %b: short month name (Jan, Feb, ...)
- %B: long month name (January, February, ...)
- %C: first two digits of four digit year (20 for dates in year 2018)
- %d: the day of the month, zero padded (01, 02, ...)
- %D: date in the format mm/dd/yy
- %e: the day of the month, space padded ( 1, 2, ...)
- %F: date in the format yyyy-mm-dd
- %h: short month name (Jan, Feb, ...)
- %H: the 24-hour of the day, zero padded (00, 01, ..., 23)
- %I: the 12-hour of the day, zero padded (01, 02, ..., 12)
- %k: the 24-hour of the day (0, 1, ..., 23)
- %l: the 12-hour of the day (1, 2, ..., 12)
- %m: the month number, zero padded (01, 02, ..., 12)
- %M: the minutes, zero padded (00, 01, ..., 59)
- %n: newline
- %p: "AM" or "PM"
- %P: "am" or "pm"
- %r: time in the format 02:32:05 PM
- %R: time in the format 14:32
- %S: seconds, zero padded (00, 01, ..., 59)
- %t: tab
- %T: time in format 14:32:05
- %w: the day of week number (0, 1, ..., 6)
- %x: date in the format 02/28/18
- %X: time in the format 14:32:05
- %y: last two digits of year, zero paddded (00, 01, ..., 99)
- %Y: the four digit year
- %$: the normal suffix for the day of the month (st, nd, rd, th, th, ...)
- %%: %
Fifth: dt = new Date(); // current date/time
dt.format("Short day: %a, Long day: %A"):
dt.format("Short month: %b, Long month: %B"):
dt.format("Day of month (zero pad): %d, Day of month (space pad): %e"):
dt.format("Year (first two digits): %C, Year (last two digits): %y"):
dt.format("AM/PM: %p, am/pm: %P"):
dt.format("24 hour: %k, 12 hour: %l"):
dt.format("Zero padded 24 hour: %H, 12 hour: %I"):
dt.format("Date format: %D"):
dt.format("Date format: %F"):
dt.format("Date: %A, %B %e%$, %Y Time: %l:%M %P"):