溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么使用Date對象

發(fā)布時間:2021-11-06 14:30:07 來源:億速云 閱讀:104 作者:iii 欄目:web開發(fā)

本篇內(nèi)容介紹了“怎么使用Date對象”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

Date對象怎么用那?

首先你要獲得Date對象

得到微-信;

var d=new Date( );

在生成日期對象的時候,不傳遞任何參數(shù)默認返回當(dāng)前時間;

var d=new Date( '2015/12/2');

在傳入?yún)?shù)的情況下,獲得的是傳入的時間;

注:這個參數(shù)是字符串形式。

一些方法:

1.d.getFullYear() 獲取當(dāng)前的年份。|| d.setFullYear(2012) 返回1970年1月1日到設(shè)定時間毫秒數(shù);

2.d.getMonth() 獲取當(dāng)前的月份(注:一個小BUG,當(dāng)前的月份從0開始)||d.setMonth(9)返回1970年1月1日到當(dāng)前年份的設(shè)定月份的毫秒數(shù);

3.d.getDate()獲取當(dāng)前的日期 ||d.setDate() 同上;

4.  getHours() 獲取時

    getMinutes() 獲取分鐘

    getSeconds() 獲取秒

各個機器獲取的時間不同,因為該方法返回的是本機的時間;并不是國際標(biāo)準時間;

5.日期的修改;

Date.parse("2015-08-24");獲取1970年到設(shè)定時間的毫秒數(shù);

d.getTime();獲取1970年到當(dāng)前時間的毫秒數(shù);

d.setTime()

    new Date(time)

    創(chuàng)建一個日期對象,并指定時間  可以指定毫秒數(shù)

或者修改time屬性, var d = new Date();  d.setTime(56521211021); 

案例:

1.將日期格式化

<!doctype html>

<html>

<head>

<meta charset="UTF-8">

<title>Document</title>

</head>

<body>

</body>

<script>

function geshihua() {

var d = new Date();

var year = d.getFullYear();

var Month = d.getMonth() + 1;

var day = d.getDate();

var str = '當(dāng)前時間是:' + year + '年' + Month + '月' + day + '日'

document.write(str);

}

geshihua()

</script>

</html>

  1. 獲取某個月的天數(shù):

<!doctype html>

<html>

<head>

<meta charset="UTF-8">

<title>Document</title>

<script>

function days(year, month) {

var str = year + '/' + month + '/1';

var d = new Date(str);

var Month = d.getMonth();

var MonthMin = d.setMonth(Month);

var MonthMin2 = d.setMonth(Month + 1);

var MonthDay = MonthMin2 - MonthMin

alert(MonthDay / 24 / 60 / 60 / 1000)

}

days('2014', '2')

</script>

</head>

<body>

</body>

</html>

  1. .計算日期差值

<!doctype html>

<html>

<head>

<meta charset="UTF-8">

<title>Document</title>

<style>

.btn{

background:none;

border: 1px solid #b6b6b6;

display: block;

height: 40px;

margin: 40px auto;

}

</style>

</head>

<body>

<div id="div1">

<input type="text" placeholder='起始年份'>

<input type="text" placeholder='起始月份'>

<input type="text" placeholder='起始日'>||

<input type="text" placeholder='終止年份'>

<input type="text" placeholder='終止月份'>

<input type="text" placeholder='終止日'>

</div>

<input type="button" value='計算日期差距' onclick='jisuanriqi()'>

</body>

<script>

function jisuanriqi() {

var oDiv = document.getElementById('div1');

var aInput = oDiv.getElementsByTagName('input');

var qishiArr = [];

var zhongzhiArr = [];

for (var i = 0; i < aInput.length; i++) {

if (i < 3) {

qishiArr[i] = aInput[i].value;

} else {

zhongzhiArr[i] = aInput[i].value;

}

}

var str1 = qishiArr.join('/');

var str2 = zhongzhiArr.join('/');

var d1 = new Date(str1);

var d2 = new Date(str2);

alert(d1 + ":" + d2)

var days = Math.abs(d1.getTime() - d2.getTime()) / 1000 / 24 / 60 / 60;

alert(days)

}

</script>

</html>

“怎么使用Date對象”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI