问题
I am trying to display the date and time in javascript based on users browser language preference. I am receiveing the date in UTC format and by using toLocaleString() i am able to convert it to browser time zone. But i also need to convert the day name and month name to browser language.
For ex
6/15/2009 1:45:30 PM -> Monday, June 15, 2009 8:45:30 PM (en-US) 6/15/2009 1:45:30 PM -> den 15 juni 2009 20:45:30 (sv-SE) 6/15/2009 1:45:30 PM -> Δευτέρα, 15 Ιουνίου 2009 8:45:30 μμ (el-GR)
回答1:
If you want consistent output regardless of browser, moment.js is a good option.
// set the desired language
moment.lang('sv');
// use one of the localized format strings
var s = moment(yourDate).format('LLLL');
There are live examples on the moment.js home page, showing all of the available languages. I don't believe there is currently support for Greek, but since it is open-source you could always add it yourself.
回答2:
Using toLocaleString you can do this:
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
// request a weekday along with a long date
var options = {weekday: "long", year: "numeric", month: "long", day: "numeric"};
alert(date.toLocaleString("de-DE", options));
// → "Donnerstag, 20. Dezember 2012"
// an application may want to use UTC and make that visible
options.timeZone = "UTC";
options.timeZoneName = "short";
alert(date.toLocaleString("en-US", options));
// → "Thursday, December 20, 2012, GMT"
来源:https://stackoverflow.com/questions/16541409/how-to-format-date-and-display-month-and-day-based-on-user-language