我正在尝试根据用户浏览器语言偏好在 javascript 中显示日期和时间。我正在接收 UTC 格式的日期,通过使用 toLocaleString() 我可以将其转换为浏览器时区。但我还需要将日期名称和月份名称转换为浏览器语言。
例如
2009 年 6 月 15 日下午 1:45:30 -> 2009 年 6 月 15 日星期一晚上 8:45:30(英文) 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)
最佳答案
使用 toLocaleString
你可以这样做:
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"
关于javascript - 如何根据用户语言格式化日期并显示月份和日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16541409/