js获取时间戳与日期格式化
Date 对象用于处理日期和时间。
创建 Date 对象的语法:
var d = new Date()
new Date().toDateString()
Date对象常用方法:
- getFullYear(): 从 Date 对象以四位数字返回年份.
- getDate(): 从 Date 对象返回一个月中的某一天 (1 ~ 31).
- getMonth(): 从 Date 对象返回月份 (0 ~ 11).
- getHours(): 返回 Date 对象的小时 (0 ~ 23).
- getMinutes(): 返回 Date 对象的分钟 (0 ~ 59).
- getSeconds(): 返回 Date 对象的秒数 (0 ~ 59).
- getMilliseconds(): 返回 Date 对象的毫秒(0 ~ 999).
- getDay(): 从 Date 对象返回一周中的某一天 (0 ~ 6)。
在Chrome浏览器console控制台操作
> new Date()
Tue Apr 30 2019 16:20:59 GMT+0800 (CST)
> new Date().getTime()
1556611808592
> new Date().getTime()/1000
1556611967.915
> parseInt(new Date().getTime()/1000)
1556611987
> new Date().getMonth()
3
> new Date().getMonth() + 1
4
> new Date().getDay()
2
> new Date().getDate()
30
格式化显示时间
以下是参考代码:
var d = new Date()
var day = d.getDate()
var month = d.getMonth() + 1
var year = d.getFullYear()
var hour = d.getHours()
var minute = d.getMinutes()
var second = d.getSeconds()
var milliseconds = d.getMilliseconds()
document.write(year + "/" + month + "/" + day)
document.write("<br/>")
document.write(year + "-" + month + "-" + day)
document.write("<br/>")
document.write(year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second+"."+milliseconds)
Reference
http://www.w3school.com.cn/jsref/jsref_obj_date.asp
[END]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)