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 badges
Add a ment  | 

2 Answers 2

Reset to default 4

You 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