js日期格式化(yy-mm-dd和yy-mm-dd hh:mm:ss)

/**

  • 時間格式化
  • 將 2020-09-23T11:54:16.000+0000 格式化成 2020-09-23
  • @param datetime 日期格式
    */
    export function format (datetime) {
    return formatWithSeperator(datetime, '-', ':')
    }

第一種:yy-mm-dd
/**

  • 時間格式化
  • 將 2019-01-01T12:00:00.000+0000 格式化成類似 2019-01-01
  • datetime:需要格式化的時間
  • dateSeprator:時間分隔符
  • @param datetime 國際化日期格式
    /
    export function formatWithSeperator (datetime, dateSeprator, timeSeprator) {
    if (datetime != null) {
    const dateMat = new Date(datetime)
    const year = dateMat.getFullYear()
    const month = dateMat.getMonth() + 1
    const day = dateMat.getDate()
    const timeFormat = year + dateSeprator + month + dateSeprator + day
    return timeFormat
    }
    }
    第二種:yy-mm-dd hh:mm:ss
    /
    *
  • 時間格式化
  • 將 2019-01-01T12:00:00.000+0000 格式化成類似 2019-01-01 12:00:00
  • 可以指定日期和時間分隔符
  • @param datetime 國際化日期格式
    */
    export function formatWithSeperator (datetime, dateSeprator, timeSeprator) {
    if (datetime != null) {
    const dateMat = new Date(datetime)
    const year = dateMat.getFullYear()
    const month = dateMat.getMonth() + 1
    const day = dateMat.getDate()
    const hh = dateMat.getHours()
    const mm = dateMat.getMinutes()
    const ss = dateMat.getSeconds()
    const timeFormat = year + dateSeprator + month + dateSeprator + day + ' ' + hh + timeSeprator + mm + timeSeprator + ss
    return timeFormat
    }
    }
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容