js 時間戳和時間格式之間的轉換。

2022-01-29 05:53:40 字數 1555 閱讀 3071

關於我的理解,簡單明瞭點:

時間戳:把乙個日期使用乙個數字表示出來,這個數字就是這個日期的秒數。

日期:就是我們常見的時間表現形式。

時間戳對於一般看時間不夠直觀明了,可是在程式的世界裡作用可大了。

獲取當前的時間戳:

1

//獲取當前時間戳(以s為單位)

2var timestamp = date.parse(new

date());

3 timestamp = timestamp / 1000;

獲取某一日期的時間戳:

1

//獲取某個時間格式的時間戳

2var stringtime = "2017-11-05 10:21:12";

3var timestamp2 = date.parse(new

date(stringtime));

4 timestamp2 = timestamp2 / 1000;

把某一時間戳轉換為日期:

1

//將當前時間換成時間格式字串

2var timesta*** = 1403058804;

3var newdate = new

date();

4 newdate.settime(timesta*** * 1000);5//

wed jun 18 2014

6console.log(newdate.todatestring());7//

wed, 18 jun 2014 02:33:24 gmt

8console.log(newdate.togmtstring());9//

2014-06-18t02:33:24.000z

10console.log(newdate.toisostring());

11//

2014-06-18t02:33:24.000z

12console.log(newdate.tojson());

13//

2023年6月18日

14console.log(newdate.tolocaledatestring());

15//

2023年6月18日 上午10:33:24

16console.log(newdate.tolocalestring());

17//

上午10:33:24

18console.log(newdate.tolocaletimestring());

19//

wed jun 18 2014 10:33:24 gmt+0800 (中國標準時間)

20console.log(newdate.tostring());

21//

10:33:24 gmt+0800 (中國標準時間)

22console.log(newdate.totimestring());

23//

wed, 18 jun 2014 02:33:24 gmt

24 console.log(newdate.toutcstring());

時間戳 與 時間格式 之間的轉換

時間戳 與 時間格式 之間的轉換 strtotime 將任何英文文字的日期或時間描述解析為 unix 時間戳 獲取伺服器當前時間 time time 時間戳 轉化為 文字的時間 時間描述 echo date y m d h i s time 通過date函式獲取年份 echo current yea...

JS將時間戳轉換時間格式

var time entry.createtime 時間戳 13位字串 var createtime new date settime time 通過時間戳設定時間值 var date createtime.format yyyy mm dd hh mm ss 測試下這種方式 newcell8.in...

時間格式轉換 時間戳

時間 附 dateformat parser new dateformat eeee,mmmm dd,yyyy dateformat formatter new dateformat eee.mm dd dateformat sdf new dateformat yyyy mm dd hh mm s...