iT邦幫忙

0

【JavaScript】 日期轉換為 年/月/日 字串

7514 2022-01-11 12:18:585344 瀏覽

JavaScript 有許多處理日期的方法toLocaleDateString() 可以將日期的標準格式,轉換為僅保留日期(不含時間)的字串格式。

toLocaleDateString() returns the date (not the time) of a date, as a string, using locale conventions:

實際操作

串接 API 時取得的資料格式,是一組標準的日期格式。

activity: {
  StartTime: '2022-01-09T01:34:29+08:00';
}

原本寫法:取得 YYYY-MM-DD 並將 '-' 轉換成 '/'

  activity.StartTime.substr(0,10).split('-').join('/')

更好的寫法:建立物件,僅保留日期並轉換為字串

  new Date(activity.StartTime).toLocaleDateString()

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言