admin管理员组文章数量:1429753
The javascript Date.toLocaleDateString() is silly.
What I need is a function that allows me to simplify the date according to preference.
It would be nice if there were a function which would read the browser date formats (plural) and take an optional parameter telling it which format to use.
I'll explain: "MM/DD/YYYY" works great for the US and anyone willing to put up with them/us. "DD/MM/YYYY" is the most mon format for people interested in a short simple date format. "Weekday, Month DayOfMonth, Year" is only useful if you want a super-long and language-dependent output.
I could use this:
var s = "/";
if(locale=='us')
var dateString = Date.getDate()+s+Date.getDay()+s+Date.getFullYear();
else
var dateString = Date.getDay()+s+Date.getDate()+s+Date.getFullYear();
But I'm looking for a more elegant solution that will allow me to store a date mask or format string so people can change the way their dates are displayed according to their own tastes. (Even the super-long language-dependent one if they like it enough.)
Should I re-prototype the Date.toString() method to interpret parameters? Is there a better way?
The javascript Date.toLocaleDateString() is silly.
What I need is a function that allows me to simplify the date according to preference.
It would be nice if there were a function which would read the browser date formats (plural) and take an optional parameter telling it which format to use.
I'll explain: "MM/DD/YYYY" works great for the US and anyone willing to put up with them/us. "DD/MM/YYYY" is the most mon format for people interested in a short simple date format. "Weekday, Month DayOfMonth, Year" is only useful if you want a super-long and language-dependent output.
I could use this:
var s = "/";
if(locale=='us')
var dateString = Date.getDate()+s+Date.getDay()+s+Date.getFullYear();
else
var dateString = Date.getDay()+s+Date.getDate()+s+Date.getFullYear();
But I'm looking for a more elegant solution that will allow me to store a date mask or format string so people can change the way their dates are displayed according to their own tastes. (Even the super-long language-dependent one if they like it enough.)
Should I re-prototype the Date.toString() method to interpret parameters? Is there a better way?
Share Improve this question asked Apr 21, 2011 at 20:39 LukeLuke 4354 silver badges12 bronze badges2 Answers
Reset to default 4You can use DateTimeFormat Api.
var now = new Date(0)
console.log(new Intl.DateTimeFormat('en-US').format(now)); //12/31/1969
console.log(new Intl.DateTimeFormat('en-GB').format(now)); //31/12/1969
See this link for Mozilla documentation: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
PROS:
- you don't have to add more libraries to you project, increasing the bundle size.
- you don't have to worry about browser support, because almost every browser supports it. https://caniuse./#search=intl
PS: if you don't care about bundle size and you want something more "user friendly" and easy to use see moment.js or luxon, they are both great libraries for date operations.
I ran into a very powerful library that takes care of dates and generic formatting:
http://blog.stevenlevithan./archives/date-time-format
(Wrong link)
http://jawe/wiki/dev/jsdateformat/home
Is pretty powerful and configurable. (It supports java-style formats that I need, such as the "MEDIUM" date format)
Moment appears to be useful and feature-full (just no Medium format): https://github./timrwood/moment
本文标签: javascript date format (international settings)Stack Overflow
版权声明:本文标题:javascript date format (international settings) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744697661a2620378.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论