javascript時間戳和日期字串相互轉換

2021-08-29 01:10:48 字數 1376 閱讀 6412

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

var stringtime = 「2014-07-10 10:21:12」;

var timestamp2 = date.parse(new date(stringtime));

timestamp2 = timestamp2 / 1000;

//2014-07-10 10:21:12的時間戳為:1404958872

console.log(stringtime + 「的時間戳為:」 + timestamp2);

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

var timesta*** = 1403058804;

var newdate = new date();

newdate.settime(timesta*** * 1000);

// wed jun 18 2014

console.log(newdate.todatestring());

// wed, 18 jun 2014 02:33:24 gmt

console.log(newdate.togmtstring());

// 2014-06-18t02:33:24.000z

console.log(newdate.toisostring());

// 2014-06-18t02:33:24.000z

console.log(newdate.tojson());

// 2023年6月18日

console.log(newdate.tolocaledatestring());

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

console.log(newdate.tolocalestring());

// 上午10:33:24

console.log(newdate.tolocaletimestring());

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

console.log(newdate.tostring());

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

console.log(newdate.totimestring());

// wed, 18 jun 2014 02:33:24 gmt

console.log(newdate.toutcstring());

date.prototype.format = function(format) ;

if (/(y+)/i.test(format))

for (var k in date)

}return format;

}console.log(newdate.format(『yyyy-mm-dd hⓜ️s』));

JavaScript時間戳與時間Date之前的轉換

gettime 方法 得到的是毫秒單位 秒的話 1000就可以了 這裡介紹幾個我常用的 tolocaledatestring 獲得日期 tolocaletimestring 獲得時間 tolocalestring chinese 獲得日期時間 時間是24小時制 tolocalestring 獲得日期...

常見的Javascript獲取時間戳

最近在做專案的時候,發現獲取時間戳的需求挺多的,通常是在做日期選擇的時候,要拿開始時間和結束時間的時間戳。每次都得google一下,還不如自己搞一搞!new date 通常會有這樣的需求,就是檢視今天的資料,那麼就需要獲取今天0點和24點的時間戳,可以通過sethours函式來進行操作 let ti...

javascript時間戳和日期字串相互轉換

js中獲得當前時間是年份和月份,形如 201208 獲取完整的日期 var date new date var year date.getfullyear var month date.getmonth 1 month month 10 0 month month var mydate year.t...