Last active
August 29, 2015 14:07
-
-
Save Ciantic/d77f02d073986ae0421f to your computer and use it in GitHub Desktop.
Parse and format iso datetime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Parse ISO datetime | |
| * | |
| * 2014-10-18 15:30:30 | |
| * 2014-10-18T15:30:30 | |
| * 2014-10-18 | |
| */ | |
| function parseIsoDatetime(dtstr) { | |
| var dt = dtstr.split(/[: T-]/).map(parseFloat); | |
| return new Date(dt[0], dt[1] - 1, dt[2], dt[3] || 0, dt[4] || 0, dt[5] || 0, 0); | |
| } | |
| var datePlaceholders = { | |
| "j" : "getDate", | |
| "n" : function (dt) { return dt.getMonth() + 1; }, | |
| "Y" : "getFullYear", | |
| "a" : function (dt) { return ["am","pm"][dt.getHours() > 11 ? 1 : 0]; }, | |
| "A" : function (dt) { return ["AM","PM"][dt.getHours() > 11 ? 1 : 0]; }, | |
| "g" : function (dt) { return "" + dt.getHours() % 12; }, | |
| "G" : function (dt) { return "" + dt.getHours(); }, | |
| "h" : function (dt) { return ("00" + dt.getHours() % 12).slice(-2); }, | |
| "H" : function (dt) { return ("00" + dt.getHours()).slice(-2); }, | |
| "i" : function (dt) { return ("00" + dt.getMinutes()).slice(-2); }, | |
| "s" : function (dt) { return ("00" + dt.getSeconds()).slice(-2); }, | |
| "D" : function (dt) { return ["su","ma","ti","ke","to","pe","la"][dt.getDay()]; }, | |
| "l" : function (dt) { return ["sunnuntai","maanantai","tiistai","keskiviikko","torstai","perjantai","lauantai"][dt.getDay()]; }, | |
| "F" : function (dt) { return ["tammikuu","helmikuu","maaliskuu","huhtikuu","toukokuu","kesäkuu","heinäkuu","elokuu","syyskuu","lokakuu","marraskuu","joulukuu"][dt.getMonth()]; }, | |
| "M" : function (dt) { return ["tammi","helmi","maalis","huhti","touko","kesä","heinä","elo","syys","loka","marras","joulu"][dt.getMonth()]; }, | |
| }; | |
| /** | |
| * Format datetime like php.net/date | |
| */ | |
| function formatDatetime(format, dt) { | |
| for (var i = format.length - 1; i>=0; i--) { | |
| var p = (i > 0 && format[i-1]) || false, | |
| c = format[i], | |
| f = datePlaceholders[c]; | |
| if (p === "\\") { | |
| format = format.substring(0, i - 1) + c + format.substring(i + 1); | |
| i--; | |
| continue; | |
| } else if (!f) { | |
| continue; | |
| } | |
| format = format.substring(0, i) + (typeof f == "string" ? dt[f]() : f(dt)) + | |
| format.substring(i + 1); | |
| } | |
| return format; | |
| } | |
| /** | |
| * Format ISO datetime helper | |
| */ | |
| function formatIsoDatetime(format, isoDt) { | |
| var dt = parseIsoDatetime(isoDt); | |
| return formatDatetime(format, dt); | |
| } | |
| // console.log(formatDatetime("j.n.Y \\k\\l\\o. H:i", new Date())); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Parse ISO datetime | |
| * | |
| * 2014-10-18 15:30:30 | |
| * 2014-10-18T15:30:30 | |
| * 2014-10-18 | |
| */ | |
| export function parseIsoDatetime(dtstr: string): Date { | |
| var dt = dtstr.split(/[: T-]/).map(parseFloat); | |
| return new Date(dt[0], dt[1] - 1, dt[2], dt[3] || 0, dt[4] || 0, dt[5] || 0, 0); | |
| } | |
| var datePlaceholders = { | |
| "j" : "getDate", | |
| "n" : function (dt: Date) { return dt.getMonth() + 1; }, | |
| "Y" : "getFullYear", | |
| "a" : function (dt: Date) { return ["am","pm"][dt.getHours() > 11 ? 1 : 0]; }, | |
| "A" : function (dt: Date) { return ["AM","PM"][dt.getHours() > 11 ? 1 : 0]; }, | |
| "g" : function (dt: Date) { return "" + dt.getHours() % 12; }, | |
| "G" : function (dt: Date) { return "" + dt.getHours(); }, | |
| "h" : function (dt: Date) { return ("00" + dt.getHours() % 12).slice(-2); }, | |
| "H" : function (dt: Date) { return ("00" + dt.getHours()).slice(-2); }, | |
| "i" : function (dt: Date) { return ("00" + dt.getMinutes()).slice(-2); }, | |
| "s" : function (dt: Date) { return ("00" + dt.getSeconds()).slice(-2); }, | |
| "D" : function (dt: Date) { return ["su","ma","ti","ke","to","pe","la"][dt.getDay()]; }, | |
| "l" : function (dt: Date) { return ["sunnuntai","maanantai","tiistai","keskiviikko","torstai","perjantai","lauantai"][dt.getDay()]; }, | |
| "F" : function (dt: Date) { return ["tammikuu","helmikuu","maaliskuu","huhtikuu","toukokuu","kesäkuu","heinäkuu","elokuu","syyskuu","lokakuu","marraskuu","joulukuu"][dt.getMonth()]; }, | |
| "M" : function (dt: Date) { return ["tammi","helmi","maalis","huhti","touko","kesä","heinä","elo","syys","loka","marras","joulu"][dt.getMonth()]; }, | |
| }; | |
| /** | |
| * Format datetime like php.net/date | |
| */ | |
| export function formatDatetime(format: string, dt: Date) { | |
| for (var i = format.length - 1; i>=0; i--) { | |
| var p = (i > 0 && format[i-1]) || false, | |
| c = format[i], | |
| f = datePlaceholders[c]; | |
| if (p === "\\") { | |
| format = format.substring(0, i - 1) + c + format.substring(i + 1); | |
| i--; | |
| continue; | |
| } else if (!f) { | |
| continue; | |
| } | |
| format = format.substring(0, i) + (typeof f == "string" ? dt[f]() : f(dt)) + | |
| format.substring(i + 1); | |
| } | |
| return format; | |
| } | |
| /** | |
| * Format ISO datetime helper | |
| */ | |
| export function formatIsoDatetime(format: string, isoDt: string) { | |
| var dt = parseIsoDatetime(isoDt); | |
| return formatDatetime(format, dt); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment