時間戳轉日期格式

2021-09-24 03:47:20 字數 1641 閱讀 6476

時間戳轉日期格式

// 轉換前格式:時間戳(秒) 、時間戳(毫秒)

// 轉換後格式:年月日時分秒、年月日、時分秒、月日時

// 轉換前 例:1555459200 、 1555459200000

// 轉換後 例:2019-04-17 08:00:00 、 2019-04-17 08:00:00

// 格式:yyyy-mm-dd hh:mm:ss 、yyyy-mm-dd、hh:mm:ss、mm-dd hh:mm:ss、mm-dd

console.log(this.timeswitch(1555459200000,'yyyy-mm-dd hh:mm:ss'))

console.log(this.timeswitch(1555459200,'yyyy-mm-dd'))

console.log(this.timeswitch(1555459200000,'hh:mm:ss'))

console.log(this.timeswitch(1555459200,'mm-dd hh:mm:ss'))

console.log(this.timeswitch(1555459200,'mm-dd'))

// 時間格式轉換

timeswitch(timestamp,formattext) else if((timestamp+'').length == 13)else

// 時間格式轉換

timestamp = new date(timestamp);

var year = timestamp.getfullyear() + ''; //年

var month = timestamp.getmonth() + 1 + ''; //月

var date = timestamp.getdate() + ''; //日

var hour = timestamp.gethours() + ''; //時

var minute = timestamp.getminutes() + ''; //分

var second = timestamp.getseconds() + '';//秒

month = month.length == 1 ? '0' + month : month;

date = date.length == 1 ? '0' + date : date;

hour = hour.length == 1 ? '0' + hour : hour;

minute = minute.length == 1 ? '0' + minute : minute;

second = second.length == 1 ? '0' + second : second;

// 結果

let result = '';

// 判斷formattext的格式

if(formattext == 'yyyy-mm-dd hh:mm:ss')else if(formattext == 'yyyy-mm-dd')else if(formattext == 'mm-dd hh:mm:ss')else if(formattext == 'mm-dd')else if(formattext == 'hh:mm:ss')else

//返回出去結果

return result;

},

PHP日期格式轉時間戳

php 提供了函式可以方便的將各種形式的日期轉換為時間戳,該類函式主要是 strtotime 將任何英文文字的日期時間描述解析為時間戳。mktime 從日期取得時間戳。strtotime strtotime 函式用於將英文文字字串表示的日期轉換為時間戳,為 date 的反函式,成功返回時間戳,否則返...

PHP日期格式轉時間戳

php 提供了函式可以方便的將各種形式的日期轉換為時間戳,該類函式主要是 strtotime 將任何英文文字的日期時間描述解析為時間戳。mktime 從日期取得時間戳。strtotime strtotime 函式用於將英文文字字串表示的日期轉換為時間戳,為 date 的反函式,成功返回時間戳,否則返...

PHP日期格式轉時間戳

php 提供了函式可以方便的將各種形式的日期轉換為時間戳,該類函式主要是 strtotime strtotime 函式用於將英文文字字串表示的日期轉換為時間戳,為 date 的反函式,成功返回時間戳,否則返回 false 語法 view source print 1 intstrtotime str...