admin管理员组文章数量:1243137
I receive date/time objects from user input, and would like to parse them to a javascript Date object. The date is in the format: 02/06/2018 00:59:03
which means second of june, 2018; UK locale.
Although this seems extremely trivial and a super wide use case scenario, I can't seem to find anything in the documentation how to specify the locale I wish to use for parsing.
What the parser does is simply assume I am using US locale format, which defaults to having first the month, then the day, and then the year, so it mixes up month and day.
Currently the only available option I see is writing my own parser, which is fine ish (it is not, of course, as I might tomorrow need another locale), but seems a little 1980ies to me.
Maybe I overlooked something in the documentation. But does anyone have any other solution? Would be greatly appreciated.
P.s. I can hardly imagine this has not been asked yet, but my search did not turn up much either.
I receive date/time objects from user input, and would like to parse them to a javascript Date object. The date is in the format: 02/06/2018 00:59:03
which means second of june, 2018; UK locale.
Although this seems extremely trivial and a super wide use case scenario, I can't seem to find anything in the documentation how to specify the locale I wish to use for parsing.
What the parser does is simply assume I am using US locale format, which defaults to having first the month, then the day, and then the year, so it mixes up month and day.
Currently the only available option I see is writing my own parser, which is fine ish (it is not, of course, as I might tomorrow need another locale), but seems a little 1980ies to me.
Maybe I overlooked something in the documentation. But does anyone have any other solution? Would be greatly appreciated.
P.s. I can hardly imagine this has not been asked yet, but my search did not turn up much either.
Share Improve this question asked Jun 10, 2018 at 8:28 Frido EmansFrido Emans 5,5864 gold badges31 silver badges44 bronze badges 1- 2 momentjs./timezone – Andreas Commented Jun 10, 2018 at 8:37
1 Answer
Reset to default 10From MDN's Date() documentation:
Note: parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies. Support for RFC 2822 format strings is by convention only. Support for ISO 8601 formats differs in that date-only strings (e.g. "1970-01-01") are treated as UTC, not local.
If your input is structured and the format is constant, writing your own parser should be straightforward. Here's an approach using a regular expression.
var dateString = '02/06/2018 00:59:03';
var dateParser = /(\d{2})\/(\d{2})\/(\d{4}) (\d{2}):(\d{2}):(\d{2})/;
var match = dateString.match(dateParser);
var date = new Date(
match[3], // year
match[2]-1, // monthIndex
match[1], // day
match[4], // hours
match[5], // minutes
match[6] //seconds
);
console.log('Input: ' + dateString);
console.log('Output (en-US): ' + date.toLocaleString('en-US'));
console.log('Output (en-GB): ' + date.toLocaleString('en-GB'));
Alternatively, string splitting would be pretty easy as well (i.e. split by a space, then split the first result by /
and the second result by :
).
本文标签: datetimeJavascript Date Parse with specific localeStack Overflow
版权声明:本文标题:datetime - Javascript Date Parse with specific locale - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740077547a2223347.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论