时间格式化及操作(js原生date对象)

# Date

new Date();                     //获取当前时间:Tue Jul 31 2018 18:21:22 GMT+0800 (中国标准时间)
Date.now();                      //获取当前毫秒时间戳:1533032436633
new Date().getDate()                     //从 Date 对象返回一个月中的某一天:1 ~ 31
new Date().getDay()                      //从 Date 对象返回一周中的某一天:0 ~ 6
new Date().getMonth()                    //从 Date 对象返回月份:0 ~ 11
new Date().getMonth()+1                //从 Date 对象返回当前月份:1 ~ 12
new Date().getFullYear()                //从 Date 对象以四位数字返回年份:1994
new Date().getHours()
new Date().getMinutes()
new Date().getMinutes()
new Date().getMilliseconds()
new Date().getTime()=== Date.now() ==new Date().valueOf()    //返回 1970 年 1 月 1 日至今的毫秒数(即Unix时间戳)

new Date().setDate()                    //设置 Date 对象中月的某一天 (1 ~ 31)。
new Date().setMonth()                    //设置 Date 对象中月份 (0 ~ 11)。
new Date().setFullYear()                //设置 Date 对象中的年份(四位数字)。
new Date().setHours()                    //设置 Date 对象中的小时 (0 ~ 23)。
new Date().setMinutes()                //设置 Date 对象中的分钟 (0 ~ 59)。
new Date().setSeconds()                //设置 Date 对象中的秒钟 (0 ~ 59)。
new Date().setMilliseconds()            //设置 Date 对象中的毫秒 (0 ~ 999)。
new Date().setTime()                    //以毫秒设置 Date 对象。
new Date().toSource()                    //返回该对象的源代码。
new Date().toString()        //把 Date 对象转换为字符串。"Tue Jul 31 2018 18:58:17 GMT+0800 (中国标准时间)"
new Date().toTimeString()    //把 Date 对象的时间部分转换为字符串。 "18:59:12 GMT+0800 (中国标准时间)" 
new Date().toDateString()                //把 Date 对象的日期部分转换为字符串。
new Date().toLocaleString()    //根据本地时间格式,把 Date 对象转换为字符串。 "2018/7/31 下午7:00:45"
new Date().toLocaleTimeString()        //根据本地时间格式,把 Date 对象的时间部分转换为字符串。
new Date().toLocaleDateString()        //根据本地时间格式,把 Date 对象的日期部分转换为字符串。
new Date().UTC()                        //根据世界时返回 1970 年 1 月 1 日 到指定日期的毫秒数。
new Date().valueOf()                    //返回 Date 对象的原始值。 1533035059223
例:↓↓↓
new Date().setDate(15)        //1531651430221 => 2018/7/15 18:43:50
new Date().setFullYear(1994)//775651810146 => 1994/7/31 18:50:10
例:↓↓↓
var d = new Date()
d.setTime(-77771564221)
console.info(new Date(d))    //Sun Jul 16 1967 04:47:15 GMT+0800 (中国标准时间)

Date.parse("July 31, 2018")            //返回1970年1月1日午夜到指定日期(字符串)的毫秒数。1532966400000 => 2018/7/31 0:0:0
Date.parse("2018-07-31")            //1532995200000 => 2018/7/31 8:0:0
Date.parse("2018-07-31 12:15:28")    //1533010528000 => 2018/7/31 12:15:28
Date.parse("2018.07.31")            //1532966400000 => 2018/7/31 0:0:0
Date.parse("2018.07.31 12:15:28")     //1533010528000 => 2018/7/31 12:15:28
Date.parse("2018/07/31")            //1532966400000 => 2018/7/31 0:0:0
Date.parse("2018/07/31 12:15:28")     //1533010528000 => 2018/7/31 12:15:28

# Date Unix时间戳

console.log(Date.parse(new Date())) //1533035416000
console.log(new Date().getTime())    //1533035416341
console.log(Date.now())                //1533035416342
console.log(new Date().valueOf())    //1533035416342
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaScript可以使用Date对象来表示日期和时间。在实际开发中,我们通常需要将Date对象转换为可阅读的字符串,这就需要用到日期格式化。 JavaScript中有很多第三方库可以实现日期格式化,如Moment.jsDate.js等。不过,本文将重点介绍原生JavaScript如何对Date对象进行格式化。 首先,我们需要知道Date对象的常见属性,如getFullYear()、getMonth()、getDate()、getHours()、getMinutes()、getSeconds()等,它们分别代表年份、月份、日、小时、分钟和秒。使用这些属性,我们可以通过拼接字符串的方式格式化日期,例如: ``` var date = new Date(); var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); var hour = date.getHours(); var minute = date.getMinutes(); var second = date.getSeconds(); var formattedDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second; ``` 上面的代码就实现了一个简单的日期格式化,将当前日期转换为"年-月-日 时:分:秒"的格式。但是,如果我们需要更加复杂的日期格式化呢?这时候,我们可以使用正则表达式来处理日期格式: ``` var date = new Date(); var pattern = /yyyy-MM-dd hh:mm:ss/; var formattedDate = pattern.exec(pattern).replace('yyyy', date.getFullYear()) .replace('MM', ("00" + (date.getMonth() + 1)).slice(-2)) .replace('dd', ("00" + date.getDate()).slice(-2)) .replace('hh', ("00" + date.getHours()).slice(-2)) .replace('mm', ("00" + date.getMinutes()).slice(-2)) .replace('ss', ("00" + date.getSeconds()).slice(-2)); ``` 上面的代码使用正则表达式匹配格式化字符串,然后使用replace()方法将对应的日期信息替换成Date对象的属性。具体来说,通过slice()方法将月、日、时、分、秒都转换成两位数的格式,以避免出现类似"2021-2-3 9:3:6"的情况。 总之,日期格式化在常见前端开发中是非常常见的,开发者可以根据实际需求自由选择使用原生JavaScript的拼接字符串方式或者第三方库来实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值