vue 毫秒数转年月日_JS实现获取毫秒值及转换成年月日时分秒的方法

本文实例讲述了js实现获取毫秒值及转换成年月日时分秒的方法。分享给大家供大家参考,具体如下:

时间日期对象

创建方式一

var date=new date();

创建方式二 兼容性最强 推荐使用

var date1=new date("2017/08-26 15:15:15");

创建方式三

var date2=new date(2017,9,18,23,15,23);

返回结果是从1970/01/01到现在的毫秒值

var date = date.now();

var date = +new date();

var date = new date().gettime();

var date = new date().valueof();

把日期解析成毫秒值

var date=date.parse("2017-05-24 18:23:14");

传入毫秒值 获取该毫秒值对应的时间日期

var date =new date(date.now());

getdate() 获取日 1-31

getday ()获取星期 0-6(0代表周日)

getmonth () 获取月 0-11(1月从0开始)

getfullyear () 获取完整年份(浏览器都支持)

gethours ()获取小时 0-23

getminutes () 获取分钟 0-59

getseconds ()获取秒 0-59

getmilliseconds () 获取毫秒 (1s = 1000ms)

gettime ()返回累计毫秒数(从1970/1/1午夜)

自己简单封装的输入毫秒值,根据不同的参数获取年月日时分秒

获取你想要的时间格式 参数(毫秒值,int) int取值为:0:年1:月2:日3:时4:分5:秒

如:

console.log(gettime(1523604904000,1));

function gettime(second,getdatetype) {

var date =new date(second);

if(getdatetype==0){

return date.getfullyear();

}else if(getdatetype==1){

if((date.getmonth()+1)<=9){

return "0"+(date.getmonth()+1);

}else {

return date.getmonth()+1;

}

}else if(getdatetype==2){

if(date.getdate()<=9){

return "0"+date.getdate();

}else {

return date.getdate();

}

}else if(getdatetype==3){

if(date.gethours()<=9){

return "0"+date.gethours();

}else {

return date.gethours();

}

}else if(getdatetype==4){

if(date.getminutes()<=9){

return "0"+date.getminutes();

}else {

return date.getminutes();

}

}else if(getdatetype==5){

return date.getseconds ();

}else {

alert("输入时间格式有误!");

return;

}

}

这里使用在线html/css/javascript代码运行工具:http://tools.jb51.net/code/htmljsrun,测试结果如下:

ps:这里再为大家推荐几款比较实用的天数计算在线工具供大家使用:

在线日期/天数计算器:

在线日期计算器/相差天数计算器:

在线日期天数差计算器:

在线天数计算器:

希望本文所述对大家javascript程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值