js/javascript: Date 日期和时间

参考:

js/javascript: Date 日期和时间对象的操作

1. 介绍

1.1 说明

Date对象,是操作日期和时间的对象。Date对象对日期和时间的操作只能通过方法。

1.2 属性

无;Date对象对日期和时间的操作只能通过方法。

二、构造函数

2.1 new Date() :返回当前的本地日期和时间

  • 参数:

  • 返回值:

{Date} 返回一个表示本地日期和时间的Date对象。

var dt = new Date();
console.log(dt); // => 返回一个表示本地日期和时间的Date对象
//2021-06-02T07:36:31.288Z

2.2 new Date(milliseconds) :把毫秒数转换为Date对象

  • 参数:

milliseconds {int} :毫秒数;表示从’1970/01/01 00:00:00’为起点,开始叠加的毫秒数。

注意:起点的时分秒还要加上当前所在的时区,北京时间的时区为东8区,起点时间实际为:‘1970/01/01 08:00:00’

  • 返回值:

{Date} 返回一个叠加后的Date对象。

var dt = new Date(1000 * 60 * 1); // 前进1分钟的毫秒数
console.log(dt); 
dt = new Date(-1000 * 60 * 1); // 倒退1分钟的毫秒数
console.log(dt); 
//1970-01-01T00:01:00.000Z
// 1969-12-31T23:59:00.000Z

2.3 new Date(dateStr) :把字符串转换为Date对象

  • 参数:

dateStr {string} :可转换为Date对象的字符串(可省略时间);字符串的格式主要有两种:

  1. yyyy/MM/dd HH:mm:ss (推荐):若省略时间,返回的Date对象的时间为 00:00:00。

  2. yyyy-MM-dd HH:mm:ss :若省略时间,返回的Date对象的时间为 08:00:00(加上本地时区)。若不省略时间,此字符串在IE中会转换失败!

  • 返回值:

{Date} 返回一个转换后的Date对象。

var dt = new Date('2014/12/25'); // yyyy/MM/dd
console.log(dt); 
dt = new Date('2014/12/25 12:00:00'); // yyyy/MM/dd HH:mm:ss
console.log(dt); 

dt = new Date('2014-12-25'); // yyyy-MM-dd
console.log(dt); 
dt = new Date('2014-12-25 12:00:00'); 
// yyyy-MM-dd HH:mm:ss (注意:此转换方式在IE中会报错!)
console.log(dt); 
// 2014-12-24T16:00:00.000Z
// 2014-12-25T04:00:00.000Z
// 2014-12-25T00:00:00.000Z
// 2014-12-25T04:00:00.000Z

2.4 new Date(year, month, opt_day, opt_hours, opt_minutes, opt_seconds, opt_milliseconds) :把年月日、时分秒转换为Date对象

  • 参数:

year {int} :年份;4位数字。如:1999、2014

month {int} :月份;2位数字。从0开始计算,0表示1月份、11表示12月份。

opt_day {int} 可选:号; 2位数字;从1开始计算,1表示1号。

opt_hours {int} 可选:时;2位数字;取值0~23。

opt_minutes {int}可选:分;2位数字;取值0~59。

opt_seconds {int}可选:秒;2未数字;取值0~59。

opt_milliseconds {int}可选:毫秒;取值0~999。

  • 返回值:

{Date} 返回一个转换后的Date对象。

var dt = new Date(2014, 11); // 2014年12月(这里输入的月份数字为11)
console.log(dt); 
dt = new Date(2014, 11, 25); // 2014年12月25日
console.log(dt); 
dt = new Date(2014, 11, 25, 15, 30, 40); // 2014年12月25日 15点30分40秒
console.log(dt); 
dt = new Date(2014, 12, 25); 
// 2014年13月25日(这里输入的月份数字为12,表示第13个月,跳转到第二年的1月)
console.log(dt); 
// 2014-11-30T16:00:00.000Z
// 2014-12-24T16:00:00.000Z
// 2014-12-25T07:30:40.000Z
// 2015-01-24T16:00:00.000Z

三、实例方法

Date对象的实例方法主要分为2种形式:本地时间UTC时间

同一个方法,一般都会有此2种时间格式操作(方法名带UTC的,就是操作UTC时间),这里主要介绍对本地时间的操作。

3.1 get方法

3.1.1 getFullYear() :返回Date对象的年份值;4位年份。
3.1.2 getMonth():返回Date对象的月份值。从0开始,所以真实月份=返回值+1 。
3.1.3 getDate():返回Date对象的月份中的日期值;值的范围1~31 。
3.1.4 getHours():返回Date对象的小时值。
3.1.5 getMinutes() :返回Date对象的分钟值。
3.1.6 getSeconds() :返回Date对象的秒数值。
3.1.7 getMilliseconds() :返回Date对象的毫秒值。
3.1.8 getDay():返回Date对象的一周中的星期值;0为星期天,1为星期一、2为星期二,依此类推
3.1.9 getTime() :返回Date对象与’1970/01/01 00:00:00’之间的毫秒值(北京时间的时区为东8区,起点时间实际为:‘1970/01/01 08:00:00’) 。

var  dt =  new  Date();
console.log(dt.getFullYear());  // => :年
console.log(dt.getMonth());  // => :月;(月份从0开始计算)
console.log(dt.getDate());  // => :日
console.log(dt.getHours());  // => :时
console.log(dt.getMinutes());  // => :分
console.log(dt.getSeconds());  // => :秒
console.log(dt.getMilliseconds());  // => :毫秒
console.log(dt.getDay());  // => :星期几的值
console.log(dt.getTime());  // =>  :
// 返回Date对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00')

// 2021
// 5
// 2
// 15
// 59
// 20
// 141
// 3
// 1622620760141

3.2 set方法

3.2.1 setFullYear(year, opt_month, opt_date) :设置Date对象的年份值;4位年份。
3.2.2setMonth(month, opt_date) :设置Date对象的月份值。0表示1月,11表示12月。
3.2.3 setDate(date):设置Date对象的月份中的日期值;值的范围1~31 。
3.2.4 setHours(hour, opt_min, opt_sec, opt_msec):设置Date对象的小时值。
3.2.5 setMinutes(min, opt_sec, opt_msec):设置Date对象的分钟值。
3.2.6 setSeconds(sec, opt_msec):设置Date对象的秒数值。
3.2.7 setMilliseconds(msec) :设置Date对象的毫秒值。

var  dt =  new  Date();
dt.setFullYear(2014);  // => 2014:年
dt.setMonth(11);  // => 11:月;实际为12月份(月份从0开始计算)
dt.setDate(25);  // => 25:日
dt.setHours(15);  // => 15:时
dt.setMinutes(30);  // => 30:分
dt.setSeconds(40);  // => 40:秒
dt.setMilliseconds(333);  // => 333:毫秒
console.log(dt);  // =>  2014年12月25日 15点30分40秒 333毫秒
//2014-12-25T07:30:40.333Z

3.3 其他方法

3.3.1 toString() :将Date转换为一个’年月日 时分秒’字符串
3.3.2 toLocaleString() :将Date转换为一个’年月日 时分秒’的本地格式字符串
3.3.3 toDateString():将Date转换为一个’年月日’字符串
3.3.4 toLocaleDateString():将Date转换为一个’年月日’的本地格式字符串
3.3.5 toTimeString():将Date转换为一个’时分秒’字符串
3.3.6 toLocaleTimeString() :将Date转换为一个’时分秒’的本地格式字符串
3.3.7 valueOf() :与getTime()一样, 返回Date对象与’1970/01/01 00:00:00’之间的毫秒值(北京时间的时区为东8区,起点时间实际为:‘1970/01/01 08:00:00’)

var  dt =  new  Date();
console.log(dt.toString());  // => :将Date转换为一个'年月日 时分秒'字符串
console.log(dt.toLocaleString());  // => :将Date转换为一个'年月日 时分秒'的本地格式字符串

console.log(dt.toDateString());  // =>  :将Date转换为一个'年月日'字符串
console.log(dt.toLocaleDateString());  // => :将Date转换为一个'年月日'的本地格式字符串

console.log(dt.toTimeString());  // =>  :将Date转换为一个'时分秒'字符串
console.log(dt.toLocaleTimeString());  // =>  :将Date转换为一个'时分秒'的本地格式字符串

console.log(dt.valueOf());  // => 
// 返回Date对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00')

//Wed Jun 02 2021 15:50:56 GMT+0800 (China Standard Time)
// 6/2/2021, 3:50:56 PM
// Wed Jun 02 2021
// 6/2/2021
// 15:50:56 GMT+0800 (China Standard Time)
// 3:50:56 PM
// 1622620256572

四、静态方法

4.1 Date.now()

说明:返回当前日期和时间的Date对象与’1970/01/01 00:00:00’之间的毫秒值(北京时间的时区为东8区,起点时间实际为:‘1970/01/01 08:00:00’)

  • 参数:

  • 返回值:

{int} :当前时间与起始时间之间的毫秒数。

console.log(Date.now());  // => 1419431519276

4.2 Date.parse(dateStr)

说明:把字符串转换为Date对象 ,然后返回此Date对象与’1970/01/01 00:00:00’之间的毫秒值(北京时间的时区为东8区,起点时间实际为:‘1970/01/01 08:00:00’)

  • 参数:

dateStr {string} :可转换为Date对象的字符串(可省略时间);字符串的格式主要有两种:

  1. yyyy/MM/dd HH:mm:ss (推荐):若省略时间,返回的Date对象的时间为 00:00:00。

  2. yyyy-MM-dd HH:mm:ss :若省略时间,返回的Date对象的时间为 08:00:00(加上本地时区)。若不省略时间,此字符串在IE中返回NaN(非数字)!

  • 返回值:

{int} 返回转换后的Date对象与起始时间之间的毫秒数。

console.log(Date.parse( '2014/12/25 12:00:00' ));  
// => 1419480000000
console.log(Date.parse( '2014-12-25 12:00:00' ));  
// => 1419480000000  (注意:此转换方式在IE中返回NaN!)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值