目次
混乱を招き、間違いを起こしやすいいくつかの方法に注意してください
日付の T と Z の説明、および標準時と北京時間の間の変換の問題
時間をもらう
現在時刻のタイムスタンプを取得する 2 つの方法
1、Date.now()
1970 年 1 月 1 日、0:00:00 (UTC、協定世界時) から現在時刻までのミリ秒数を取得します。
2、new Date().getTime()
1970 年 1 月 1 日の 0:00:00 (UTC、つまり協定世界時) から Date オブジェクトで表される時刻までのミリ秒数を取得します。(それ以前の時間は負の数で表されます)
//获取当前时间-时间戳
const currentTime1 = Date.now()
console.log('currentTime1: ', currentTime1)
const currentTime12 = new Date().getTime()
console.log('currentTime12: ', currentTime12)
年、月、日、時、分、秒を取得します
Date() コンストラクターのパラメーターの説明
まず、Date コンストラクターのパラメーターには次のような状況があることを知っておく必要があります。
1、日付()
2. 日付(時刻文字列)
3. 日付(タイムスタンプ)
4. 日付(年、月、日、時、分、秒)
時間を得る方法
1. 年を取得します getFull Year()
2. 月を取得する
3. 日付を取得する
4. 曜日を取得する
5.入手時
6. ポイントを獲得する
7. 秒数を取得する
例
// Date()不传递参数
const date0 = new Date()
const year0 = date0.getFullYear()
const month0 = date0.getMonth() + 1
const day0 = date0.getDate()
const week0 = date0.getDay()
const hour0 = date0.getHours();
const minute0 = date0.getMinutes();
const second0 = date0.getSeconds();
console.log('year: ', year0, ' month: ', month0, ' day: ', day0, ' week: ', week0, ' hour: ', hour0,
' minute: ',
minute0, ' second: ', second0)
// Date传递时间字符串--年月日的形式
const date = new Date('2022-09-7 22:09:09')
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const week = date.getDay()
const hour = date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
console.log('year: ', year, ' month: ', month, ' day: ', day, ' week: ', week, ' hour: ', hour, ' minute: ',
minute, ' second: ', second)
// Date传递时间字符串--时间戳的形式
const date2 = new Date(1662561716215)
const year2 = date2.getFullYear()
const month2 = date2.getMonth() + 1
const day2 = date2.getDate()
const week2 = date2.getDay()
const hour2 = date2.getHours();
const minute2 = date2.getMinutes();
const second2 = date2.getSeconds();
console.log('year: ', year2, ' month: ', month2, ' day: ', day2, ' week: ', week2, ' hour: ', hour2,
' minute: ', minute2, ' second: ', second2)
// Date传递时间字符串--月日年时分秒的形式
const date3 = new Date('September 7, 2022 11:45:12');
const year3 = date3.getFullYear()
const month3 = date3.getMonth() + 1
const day3 = date3.getDate()
const week3 = date3.getDay()
const hour3 = date3.getHours();
const minute3 = date3.getMinutes();
const second3 = date3.getSeconds();
console.log('year: ', year3, ' month: ', month3, ' day: ', day3, ' week: ', week3, ' hour: ', hour3,
' minute: ', minute3, ' second: ', second3)
// Date传递年,月,日,时,分,秒的具体数值
const date4 = new Date(2022, 8, 7, 22, 48, 13); //注意传递月份的时候需要实际月份减1
const year4 = date4.getFullYear()
const month4 = date4.getMonth() + 1
const day4 = date4.getDate()
const week4 = date4.getDay()
const hour4 = date4.getHours();
const minute4 = date4.getMinutes();
const second4 = date4.getSeconds();
console.log('year: ', year4, ' month: ', month4, ' day: ', day4, ' week: ', week4, ' hour: ', hour4,
' minute: ', minute4, ' second: ', second4)
混乱を招き、間違いを起こしやすいいくつかの方法に注意してください
1. getFull Year() と get Year() の違い
getFull Year() は、Date オブジェクトの完全な年 (4 桁の年) を取得します。
get Year() は相対時刻 (1900 を基準とした時刻) を取得します。
2. getDate() と getDay() の違い
getDate() は月の日付を取得します。値の範囲は 1 ~ 31 です。
getDay() は曜日を取得します。値の範囲は 0 ~ 6 で、0 は日曜日を意味します。
3. getMonth() で 1 を加算する必要があるのはなぜですか?
getMonth() は Date オブジェクトの月を返します。値の範囲は 0 ~ 11 です。0 は年の最初の月を表すため、実際の月を取得するには getMonth()+1 が必要です。
時間換算
// 时间戳转换为年月日时分秒
const date = new Date(1662561716215)
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
const time =
`${year}-${dealDate(month)}-${dealDate(day)} ${dealDate(hour)}:${dealDate(minute)}:${dealDate(second)}`
console.log('time: ', time)
function dealDate(value) {
return value < 10 ? '0' + value : value
}
日付の T と Z の説明、および標準時と北京時間の間の変換の問題
Tはセパレータの略です
Zはタイムゾーンを表します
国際標準時間の形式は次のとおりです: 2022-09-07T23:00:00.000Z
北京時間の形式: 2022-09-07 00:00:00
北京時間は東八区にあるため、標準時間に 8 時間を加えた東八区時間、つまり北京時間となります。
したがって、16時以降の標準時を北京時間に換算すると1時間が増えることになります。
2022-09-07T15:59:59.000ZgetDate() で取得した値は 7 です
2022-09-07T16:00:00.000ZgetDate() で取得した値は 8 です
const date = new Date('2022-09-07T15:59:59.000Z')
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const week = date.getDay()
const hour = date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
console.log('2022-09-07T15:59:59.000Z year: ', year, ' month: ', month, ' day: ', day, ' week: ', week, ' hour: ', hour, ' minute: ',
minute, ' second: ', second)
const date2 = new Date('2022-09-07T16:00:00.000Z')
const year2 = date2.getFullYear()
const month2 = date2.getMonth() + 1
const day2 = date2.getDate()
const week2 = date2.getDay()
const hour2 = date2.getHours();
const minute2 = date2.getMinutes();
const second2 = date2.getSeconds();
console.log('2022-09-07T16:00:00.000Z year: ', year2, ' month: ', month2, ' day: ', day2, ' week: ', week2, ' hour: ', hour2,
' minute: ', minute2, ' second: ', second2)