Parsowanie daty ISO 8601 w Javascript
Potrzebujesz pomocy / wskazówek dotyczących konwersji daty ISO 8601 z następującą strukturą do javascript.
CCYY-MM-DDThh:mm:ssTZD
Chciałbym sformatować datę TAK:
January 28, 2011 - 7:30PM EST
Chciałbym utrzymać To rozwiązanie tak czyste i minimalne, jak to tylko możliwe. 6 answers
Datejs może parse following, warto wypróbować.
Date.parse('1997-07-16T19:20:15') // ISO 8601 Formats
Date.parse('1997-07-16T19:20:30+01:00') // ISO 8601 with Timezone offset
Edit: Regex version
x = "2011-01-28T19:30:00EST"
MM = ["January", "February","March","April","May","June","July","August","September","October","November", "December"]
xx = x.replace(
/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):\d{2}(\w{3})/,
function($0,$1,$2,$3,$4,$5,$6){
return MM[$2-1]+" "+$3+", "+$1+" - "+$4%12+":"+$5+(+$4>12?"PM":"AM")+" "+$6
}
)
Wynik
January 28, 2011 - 7:30PM EST
Edit2: zmieniłem strefę czasową na EST i teraz mam następujące
x = "2011-01-28T19:30:00-05:00"
MM = {Jan:"January", Feb:"February", Mar:"March", Apr:"April", May:"May", Jun:"June", Jul:"July", Aug:"August", Sep:"September", Oct:"October", Nov:"November", Dec:"December"}
xx = String(new Date(x)).replace(
/\w{3} (\w{3}) (\d{2}) (\d{4}) (\d{2}):(\d{2}):[^(]+\(([A-Z]{3})\)/,
function($0,$1,$2,$3,$4,$5,$6){
return MM[$1]+" "+$2+", "+$3+" - "+$4%12+":"+$5+(+$4>12?"PM":"AM")+" "+$6
}
)
Return
January 28, 2011 - 7:30PM EST
W zasadzie
String(new Date(x))
Return
Fri Jan 28 2011 19:30:00 GMT-0500 (EST)
Regex CZĘŚCI TYLKO konwertowanie powyżej ciąg do wymaganego formatu.
January 28, 2011 - 7:30PM EST
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-08 08:48:21
Obiekt Date obsługuje 8601 jako pierwszy parametr:
var d = new Date("2014-04-07T13:58:10.104Z");
console.log(d.toString());
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-12-16 12:15:48
Jeśli chcesz zachować to proste, to powinno wystarczyć:
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);
}
Notatka parseFloat jest konieczny, parseInt nie zawsze działa. Mapa wymaga IE9 lub nowszego.
Działa dla formatów:
- 2014-12-28 15:30:30
- 2014-12-28t15: 30: 30
- 2014-12-28
Nie dotyczy stref czasowych, zobacz inne odpowiedzi na ich temat.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-17 23:09:58
Może, możesz użyć chwili.js, który moim zdaniem jest najlepszą biblioteką JavaScript do parsowania, formatowania i pracy z datami po stronie klienta. Przydałoby się coś w stylu:
var momentDate = moment('1890-09-30T23:59:59+01:16:20', 'YYYY-MM-DDTHH:mm:ss+-HH:mm:ss');
var jsDate = momentDate.toDate();
// Now, you can run any JavaScript Date method
jsDate.toLocaleString();
Zaletą korzystania z biblioteki jak moment.js polega na tym, że Twój kod będzie działał idealnie nawet w starszych przeglądarkach, takich jak IE 8+.
Oto dokumentacja dotycząca metod parsowania: https://momentjs.com/docs/#/parsing/
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-22 16:28:25
Wygląda na moment.js jest najbardziej popularny i z aktywnym rozwojem:
moment("2010-01-01T05:06:07", moment.ISO_8601);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2021-01-03 03:00:33
Zgodnie z MSDN, obiekt JavaScript Date nie dostarcza żadnych specyficznych metod formatowania daty (Jak widać w innych językach programowania). Możesz jednak użyć kilku metod Date
i formatowania, aby osiągnąć swój cel:
function dateToString (date) {
// Use an array to format the month numbers
var months = [
"January",
"February",
"March",
...
];
// Use an object to format the timezone identifiers
var timeZones = {
"360": "EST",
...
};
var month = months[date.getMonth()];
var day = date.getDate();
var year = date.getFullYear();
var hours = date.getHours();
var minutes = date.getMinutes();
var time = (hours > 11 ? (hours - 11) : (hours + 1)) + ":" + minutes + (hours > 11 ? "PM" : "AM");
var timezone = timeZones[date.getTimezoneOffset()];
// Returns formatted date as string (e.g. January 28, 2011 - 7:30PM EST)
return month + " " + day + ", " + year + " - " + time + " " + timezone;
}
var date = new Date("2011-01-28T19:30:00-05:00");
alert(dateToString(date));
Możesz nawet pójść o krok dalej i obejść metodę Date.toString()
:
function dateToString () { // No date argument this time
// Use an array to format the month numbers
var months = [
"January",
"February",
"March",
...
];
// Use an object to format the timezone identifiers
var timeZones = {
"360": "EST",
...
};
var month = months[*this*.getMonth()];
var day = *this*.getDate();
var year = *this*.getFullYear();
var hours = *this*.getHours();
var minutes = *this*.getMinutes();
var time = (hours > 11 ? (hours - 11) : (hours + 1)) + ":" + minutes + (hours > 11 ? "PM" : "AM");
var timezone = timeZones[*this*.getTimezoneOffset()];
// Returns formatted date as string (e.g. January 28, 2011 - 7:30PM EST)
return month + " " + day + ", " + year + " - " + time + " " + timezone;
}
var date = new Date("2011-01-28T19:30:00-05:00");
Date.prototype.toString = dateToString;
alert(date.toString());
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-04 22:22:51