js對時間的操作

2021-09-26 18:40:32 字數 1721 閱讀 7165

獲取當前時間

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

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( ); // 獲取日期與時間

時間格式化

// 對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");

C 對時間的操作

獲取當前星期的開始日期和結束日期 private void timebe out string bdate,out string edate else if firstday.dayofweek dayofweek.monday else if firstday.dayofweek dayofwee...

js對時間的一些操作

new date thu dec 27 2018 12 16 16 gmt 0800 中國標準時間 new date 2018 1 1,12 20 20 1258454512000 轉換時間格式的字串或者1970到現在的毫秒數 為中國標準時間 new date gethours 獲取當前時間的小時 ...

C 中對時間的操作

1 datetime 數字型 system.datetime currenttime new system.datetime 1.1 取當前年月日時分秒 currenttime system.datetime.now 1.2 取當前年 int 年 currenttime.year 1.3 取當前月 ...