How to format date and display month and day based on user language

后端 未结 2 1945
悲哀的现实
悲哀的现实 2020-12-22 01:55

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

2条回答
  •  心在旅途
    2020-12-22 02:44

    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"
    

提交回复
热议问题