Get String in YYYYMMDD format from JS date object?

后端 未结 30 2457
一个人的身影
一个人的身影 2020-11-22 10:47

I\'m trying to use JS to turn a date object into a string in YYYYMMDD format. Is there an easier way than concatenating Date.getYear()

30条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 11:25

    // UTC/GMT 0
    document.write('UTC/GMT 0: ' + (new Date()).toISOString().slice(0, 19).replace(/[^0-9]/g, "")); // 20150812013509
    
    // Client local time
    document.write('
    Local time: ' + (new Date(Date.now()-(new Date()).getTimezoneOffset() * 60000)).toISOString().slice(0, 19).replace(/[^0-9]/g, "")); // 20150812113509

提交回复
热议问题