js 獲取當前時間特定格式

2021-08-28 21:50:41 字數 1729 閱讀 4085

1.獲取當前時間

var mydate = new date();
2.獲取時間中的年月日時分秒

mydate.getyear();        // 獲取當前年份(2位)

mydate.getfullyear(); // 獲取完整的年份(4位,1970-????)

mydate.getmonth(); // 獲取當前月份(0-11,0代表1月)

mydate.getdate(); // 獲取當前日(1-31)

mydate.getday(); // 獲取當前星期x(0-6,0代表星期天)

mydate.gettime(); // 獲取當前時間(從1970.1.1開始的毫秒數)

mydate.gethours(); // 獲取當前小時數(0-23)

mydate.getminutes(); // 獲取當前分鐘數(0-59)

mydate.getseconds(); // 獲取當前秒數(0-59)

mydate.getmilliseconds(); // 獲取當前毫秒數(0-999)

mydate.tolocaledatestring(); // 獲取當前日期

var mytime=mydate.tolocaletimestring(); // 獲取當前時間

mydate.tolocalestring( ); // 獲取日期與時間

3.時間的格式化

// 對date的擴充套件,將 date 轉化為指定格式的string

// 月(m)、日(d)、小時(h)、分(m)、秒(s)、季度(q) 可以用 1-2 個佔位符,

// 年(y)可以用 1-4 個佔位符,毫秒(s)只能用 1 個佔位符(是 1-3 位的數字)

// 例子:

// (new date()).format("yyyy-mm-dd hh:mm:ss.s") ==> 2006-07-02 08:09:04.423

// (new date()).format("yyyy-m-d h:m:s.s") ==> 2006-7-2 8:9:4.18

date.prototype.format = function (fmt) ;

if (/(y+)/.test(fmt))

fmt = fmt.replace(regexp.$1, (this.getfullyear() + "").substr(4 - regexp.$1.length));

for (var k in o)

if (new regexp("(" + k + ")").test(fmt)) fmt = fmt.replace(regexp.$1, (regexp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));

return fmt;

}

呼叫:

var time1 = new date().format("yyyy-mm-dd");

var time2 = new date().format("yyyy-mm-dd hh:mm:ss");

python 獲取特定格式的時間戳

首先用time.time獲取時間戳 通過 time模組的localtime time.time 函式獲取處理過的時間結構體 元組 示例如下 time.struct time tm year 2019,tm mon 9,tm mday 11,tm hour 12,tm min 12,tm sec 0,...

js中按照指定格式獲取當前系統時間

在js 中,var nowtime new date 可獲取當前時間,但是格式通常不是我們想要的,如何更改?關於日期操作,可查詢js中的date物件 var nowtime new date console.log nowtime mon sep 03 2018 14 57 36 gmt 0800 ...

js 獲取當前時間格式怎麼轉換

查詢操作 例項化物件 var dateval new date search date.getvalue 格式轉換 var yy dateval.getyear if yy 1900 yy yy 1900 var mm dateval.getmonth 1 if mm 10 mm 0 mm var ...