js中關於Date型別的演算法和字串之間的轉換

2021-09-02 10:21:10 字數 974 閱讀 5262

1.將日期的字串轉化成毫秒數:

var converted = date.parse('2009/01/05');  

console.log(converted); 

var converted = date.parse('2009-01-05');  

console.log(converted); 

2.將字串格式日期轉化成date型別

s="2015-03-04";

var m1 = new date(s.replace(/-/g,"/"));

console.log(m1);

wed mar 04 2015 00:00:00 gmt+0800 (中國標準時間)

s="2015/03/04";

var m1 = new date(s.replace(/-/g,"/"));

console.log(m1);

wed mar 04 2015 00:00:00 gmt+0800 (中國標準時間)

s="2015/03/04 12:12:12";

var m1 = new date(s.replace(/-/g,"/"));

console.log(m1);

wed mar 04 2015 12:12:12 gmt+0800 (中國標準時間)

3.日期之間可以直接相加減

var start="2015/02/25";

var end="2015/03/04";

var m1 = new date(end.replace(/-/g,"/"));//轉化成日期格式;

var m2 = new date(start.replace(/-/g,"/"));

var n = m1 - m2;//直接相減得到的是毫秒數

console.log(n);

var day = n/1000/60/60/24;//得到天數;

console.log(day);

js中Math和Date物件

1.math 普通函式,就是乙個物件 比較math.min math.max math.ceil 向上取整 math.floor 向下取整 math.round math.random 隨機數,0 1之間.不包括1,沒有引數 math.log 對數 math.sin 正弦函式 math.cos 余弦...

js中的Date的設定和獲取

js中獲取時間首先需建立以個時間物件 語法為 var odate new date 獲取當前時間 gethours 獲取當前小時數 getminutes 獲取當前分鐘數 getseconds 獲取當前的秒數 獲取當前時期的時間 getfullyear 獲取4位數的年份 getmonth 獲取月份,從...

js中的date 函式

1 獲取當前時間 let date new date 2 獲取當前的年份 let year date.getfullyear 3 獲取當前月份 let month date.getmonth 1 4 獲取當前日期 let nowdate date.getdate 5 獲取當前時分秒 let h da...