js时间,标准时间,时间戳,年月日时间格式的转换

1、yyyy-MM-dd转时间戳

var date = '1999-09-21'
var res = Date.parse(new Date(date))/1000 //937872000

2、标准时间转yyyy-MM-dd

var res = new Date().toLocaleDateString().slice().replace(/\//g, '-') //"2020-9-10"

3、标准时间转 yyyy-MM-dd HH:mm:ss

var date= new Date() //Thu Sep 10 2020 17:22:32 GMT+0800 (中国标准时间) 
var year=date.getFullYear();  //取得4位数的年份
var month=date.getMonth()+1;  //取得日期中的月份,其中0表示1月,11表示12月
var day=date.getDate();      //返回日期月份中的天数(1到31)
var hour=date.getHours();     //返回日期中的小时数(0到23)
var minute=date.getMinutes(); //返回日期中的分钟数(0到59)
var second=date.getSeconds(); //返回日期中的秒数(0到59)
var res = year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second; //2020-9-10 17:22:32

4、时间戳转yyyy-MM-dd HH:mm:ss

var date= new Date(937872000 ) //标准时间
//先把时间戳转为标准时间,然后再执行3中步骤

5、时间戳转时间

var timestamp = 937872000 //1中获取的时间戳
var res = new Date(parseInt(timestamp) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ") // 1999/9/21 上午8:00:00
var timestamp1 = Date.parse(new Date) //精确到秒 1599730486000
var timestamp2=new Date().getTime() //精确到毫秒 1599730486864
var res2 = new Date(parseInt(timestamp1)).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ") // 结果都是2020/9/10 下午5:32:01

6、时间戳转年月日时分秒

将4和3结合后中最后一句变为

var res = year+"年"+month+"月"+day+"日 "+hour+"时"+minute+"分"+second+"秒" // 2020年9月10日 17时57分8秒
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值