Format JavaScript date as yyyy-mm-dd

后端 未结 30 2672
再見小時候
再見小時候 2020-11-22 01:28

I have a date with the format Sun May 11,2014. How can I convert it to 2014-05-11 using JavaScript?

相关标签:
30条回答
  • 2020-11-22 01:57

    None of these answers quite satisfied me. I wanted a cross-platform solution that gave me the day in the local timezone without using any external libraries.

    This is what I came up with:

    function localDay(time) {
      var minutesOffset = time.getTimezoneOffset()
      var millisecondsOffset = minutesOffset*60*1000
      var local = new Date(time - millisecondsOffset)
      return local.toISOString().substr(0, 10)
    }
    

    That should return the day of the date, in YYYY-MM-DD format, in the timezone the date references.

    So for example, localDay(new Date("2017-08-24T03:29:22.099Z")) will return "2017-08-23" even though it's already the 24th at UTC.

    You'll need to polyfill Date.prototype.toISOString for it to work in Internet Explorer 8, but it should be supported everywhere else.

    0 讨论(0)
  • 2020-11-22 01:58

    I use this way to get the date in format yyyy-mm-dd :)

    var todayDate = new Date().toISOString().slice(0,10);
    
    0 讨论(0)
  • 2020-11-22 02:01

    2020 ANSWER

    You can use the native .toLocaleDateString() function which supports several useful params like locale (to select a format like MM/DD/YYYY or YYYY/MM/DD), timezone (to convert the date) and formats details options (eg: 1 vs 01 vs January).

    Examples

    new Date().toLocaleDateString() // 8/19/2020
    
    new Date().toLocaleDateString('en-US', {year: 'numeric', month: '2-digit', day: '2-digit'}); // 08/19/2020 (month and day with two digits)
    
    new Date().toLocaleDateString('en-ZA'); // 2020/08/19 (year/month/day) notice the different locale
    
    new Date().toLocaleDateString('en-CA'); // 2020-08-19 (year-month-day) notice the different locale
    
    new Date().toLocaleString("en-US", {timeZone: "America/New_York"}); // 8/19/2020, 9:29:51 AM. (date and time in a specific timezone)
    
    new Date().toLocaleString("en-US", {hour: '2-digit', hour12: false, timeZone: "America/New_York"});  // 09 (just the hour)
    

    Notice that sometimes to output a date in your specific desire format, you have to find a compatible locale with that format. You can find the locale examples here: https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_tolocalestring_date_all

    Please notice that locale just change the format, if you want to transform a specific date to a specific country or city time equivalent then you need to use the timezone param.

    0 讨论(0)
  • 2020-11-22 02:01

    Date.js is great for this.

    require("datejs")
    (new Date()).toString("yyyy-MM-dd")
    
    0 讨论(0)
  • 2020-11-22 02:02

    Shortest

    .toJSON().slice(0,10);
    

    var d = new Date('Sun May 11,2014' +' UTC');   // Parse as UTC
    let str = d.toJSON().slice(0,10);              // Show as UTC
    
    console.log(str);

    0 讨论(0)
  • 2020-11-22 02:02

    I suggest using something like formatDate-js instead of trying to replicate it every time. Just use a library that supports all the major strftime actions.

    new Date().format("%Y-%m-%d")
    
    0 讨论(0)
提交回复
热议问题