日期格式轉換

2022-02-13 23:23:21 字數 1433 閱讀 9491

1.把時間戳或帶格式的日期轉換成標準格式

new date()//轉換成標準日期格式

如:2013-11-11,先用new date('2013-11-11')轉換成標準格式:mon nov 11 2013 08:00:00 gmt+0800 (中國標準時間)

如:1398250549490,先用new date(1535760000000)轉換成標準格式:sat sep 01 2018 08:00:00 gmt+0800 (中國標準時間)

將時間戳轉換為指定的格式的方法:比如轉換成 年月日時分秒 這種格式:yyyy-mm-dd hh:mm:ss 或者 yyyy-mm-dd。

function dateformat (date, fmt) 

if (/(y+)/.test(fmt)) fmt = fmt.replace(regexp.$1, (date.getfullyear() + '').substr(4 - regexp.$1.length))

for (let k in o)

return fmt

}

引用方法

dateformat(1535760000000, 'yyyy-mm-dd')//將前面的數字日期的格式轉換成yyyy-mm-dd樣式
dateformat(1535760000000, 'yyyy-mm-dd hh:mm:ss')//將前面的數字日期的格式轉換成yyyy-mm-dd hh:mm:ss樣式2

2.把日期轉換成時間戳,獲取精準的時間戳

new date(time).gettime()

如:console.log(new date('2013-11-11 13:34:04').gettime())

3.判斷日期是否為昨天

// 判斷日期是否為昨天

function isyestday (thedate)

//呼叫

let time = new date('2013-11-11')

isyestday(time) //false time為標準時間格式 mon nov 11 2013 08:00:00 gmt+0800 (中國標準時間)

4.js獲取今天0點的時間戳和23.59分的時間戳

let starttime1 = new date(new date(new date().tolocaledatestring()).gettime()); //

當天0點

let endtime1 = new date(new date(new date().tolocaledatestring()).gettime() +24 * 60 * 60 * 1000 -1);//

當天23:59

python 日期 格式轉換 英文 日期格式轉換

一 date型轉字串 filter date date,yyyymm filter date time,hh mm 在控制器中使用必須注入 filter 模組 scope.dt1 new date 控制器中使用 scope.dt2 filter date scope.dt1,yyyy mm dd h...

日期格式轉換

像 mmmm d,yyyy 這樣的日期格式需要設定相應的 locale 才可以被正確識別。例 x june 3,2012 dateformatter setlocale nslocalealloc initwithlocaleidentifier en us autorelease dateform...

日期格式轉換

2015.1.11 201501 2015.12.18 201518 substr x,start,number instr x,object number,next select t.tdsyzmc,t.nsrglm,t.tdyt,t.tdxz,t.zdqdjg,case when instr s...