js时间和时间戳之间如何转换(汇总)

一、总结

一句话总结:

1、js中通过new Date()来获取时间对象,

2、这个时间对象可以通过getTime()方法获取时间戳,

3、也可以通过getYear()、getMonth()获取年月,

4、也可以通过toTimeString().substr(0, 8));的方法获取时分秒。

1、js中怎么获取日期对象?

解答:new Date();

2、js中如何将日期对象转化为时间戳?(四种方法)

解答:a、Number强制转换Number(new Date()) ;,b、通过原型方法getTime()直接获得当前时间的毫秒值new Date().getTime(); c、通过valueOf()函数返回指定对象的原始值获得准确的时间戳值(new Date()).valueOf(); d、通过Date.parse方法Date.parse(new Date());不推荐这种办法,毫秒级别的数值被转化为000

3、js中如何将时间戳转化为时间?

解答:直接用 new Date(时间戳) 格式转化获得当前时间,var timestamp4 = new Date(1472048779952);

4、js中如何获取一个时间对象的时分秒?

解答:timeObject.toTimeString().substr(0, 8));

5、时间对象的toLocaleDateString()方法是干嘛的?

解答:把时间对象转化为本地时间啊,不过不同浏览器效果不同

6、js中如何通过时间对象获取年?

解答:y = now.getFullYear(),

7、js中如何通过时间对象获取月?

解答:m = now.getMonth() + 1,

8、js中如何通过时间对象获取日?

解答:d = now.getDate();
 

9、js中获取时间对象的年月日方法的前缀是什么?

解答:get
 

10、js时间对象中的time是什么?

解答:时间戳

二、js时间和时间戳之间如何转换

一:时间转时间戳:

javascript获得时间戳的方法有四种,都是通过实例化时间对象 new Date() 来进一步获取当前的时间戳

1.var timestamp1 = Date.parse(new Date()); // 结果:1477808630000 不推荐这种办法,毫秒级别的数值被转化为000

console.log(timestamp1);

2.var timestamp2 = (new Date()).valueOf(); // 结果:1477808630404 通过valueOf()函数返回指定对象的原始值获得准确的时间戳值

console.log(timestamp2);

3.var timestamp3 = new Date().getTime(); // 结果:1477808630404 ,通过原型方法直接获得当前时间的毫秒值,准确

console.log(timestamp3);

4.var timetamp4 = Number(new Date()) ; //结果:1477808630404 ,将时间转化为一个number类型的数值,即时间戳

console.log(timetamp4);

打印结果 如下:

二,时间戳转时间

var timestamp4 = new Date(1472048779952);//直接用 new Date(时间戳) 格式转化获得当前时间

console.log(timestamp4);

console.log(timestamp4.toLocaleDateString().replace(/\//g, "-") + " " + timestamp4.toTimeString().substr(0, 8)); //再利用拼接正则等手段转化为yyyy-MM-dd hh:mm:ss 格式

效果如下:

不过这样转换在某些浏览器上会出现不理想的效果,因为toLocaleDateString()方法是因浏览器而异的,比如 IE为2016年8月24日 22:26:19 格式 搜狗为Wednesday, August 24, 2016 22:39:42

可以通过分别获取时间的年月日进行拼接,比如:

function getdate() {
var now = new Date(),
y = now.getFullYear(),
m = now.getMonth() + 1,
d = now.getDate();
return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d) + " " + now.toTimeString().substr(0, 8);
}
 

三、测试题-简答题

1、js中怎么获取日期对象?

解答:new Date();

2、js中如何将日期对象转化为时间戳?(四种方法)

解答:a、Number强制转换Number(new Date()) ;,b、通过原型方法getTime()直接获得当前时间的毫秒值new Date().getTime(); c、通过valueOf()函数返回指定对象的原始值获得准确的时间戳值(new Date()).valueOf(); d、通过Date.parse方法Date.parse(new Date());不推荐这种办法,毫秒级别的数值被转化为000

3、js中如何将时间戳转化为时间?

解答:直接用 new Date(时间戳) 格式转化获得当前时间,var timestamp4 = new Date(1472048779952);

4、js中如何获取一个时间对象的时分秒?

解答:timeObject.toTimeString().substr(0, 8));

5、时间对象的toLocaleDateString()方法是干嘛的?

解答:把时间对象转化为本地时间啊,不过不同浏览器效果不同

6、js中如何通过时间对象获取年?

解答:y = now.getFullYear(),

7、js中如何通过时间对象获取月?

解答:m = now.getMonth() + 1,

8、js中如何通过时间对象获取日?

解答:d = now.getDate();
 

9、js中获取时间对象的年月日方法的前缀是什么?

解答:get
 

10、js时间对象中的time是什么?

解答:时间戳
 
 
 
 
 
 

js时间和时间戳之间如何转换(汇总)的更多相关文章

  1. js时间与时间戳之间的转换操作,返回天、小时、分,全家桶

    1.将时间戳转换成时间 var formatDate = function(d) {  var now = new Date(d); var year = now.getFullYear(); var ...

  2. python—时间与时间戳之间的转换

    python-时间与时间戳之间的转换 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块 ...

  3. 【python-时间戳】时间与时间戳之间的转换

    对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块,具体的操作有如下的几种: 将时间转换 ...

  4. python 时间与时间戳之间的转换

    https://blog.csdn.net/kl28978113/article/details/79271518 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运 ...

  5. python——时间与时间戳之间的转换

    http://blog.csdn.net/google19890102/article/details/51355282

  6. JS 时间字符串与时间戳之间的转换

    1.当前时间换时间戳 var timestamp = parseInt(new Date().getTime()/1000); // 当前时间戳 document.write(timestamp); ...

  7. Python时间、日期、时间戳之间的转换

    一.字符串与为时间字符串之间的互相转换 方法:time模块下的strptime方法 a = "2012-11-11 23:40:00" # 字符串转换为时间字符串 import t ...

  8. js 时间与时间戳的转换

      一:时间转时间戳:javascript获得时间戳的方法有四种,都是通过实例化时间对象 new Date() 来进一步获取当前的时间戳 1.var timestamp1 = Date.parse(n ...

  9. python 时间字符串和时间戳之间的转换

    https://blog.csdn.net/qq_37193537/article/details/78987949   1.将字符串的时间转换为时间戳    方法:        a = " ...

随机推荐

  1. System and method for controlling switching between VMM and VM using enabling value of VMM timer indicator and VMM timer value having a specified time

    In one embodiment, a method includes transitioning control to a virtual machine (VM) from a virtual ...

  2. CORS原理

    http://blog.csdn.net/renfufei/article/details/51675148 https://html.spec.whatwg.org/multipage/infras ...

  3. method initializationerror not found:JUnit4单元測试报错问题

           今天使用JUnit 4进行单元測试时,測试程序一直执行不起来,报method initializationerror not found错误.例如以下:            网上说版本 ...

  4. PHP用socket连接SMTP服务器发送邮件

    PHP用socket连接SMTP服务器发送邮件 PHP用socket连接SMTP服务器发送邮件学习实验记录: 分析与SMTP会话的一般流程 1. HELO XXX \r\n //XXX就是自己起个名字 ...

  5. layout-maxWidth属性用法

    对于maxWidth属性,相信大家都不陌生.不过,今天我遇到了一个问题,就是当我希望一个relayout的宽度有个最大值的时候,用maxWidth却没办法实现.这里总结下maxWidth 的用法 1. ...

  6. 1.1 Introduction中 Apache Kafka™ is a distributed streaming platform. What exactly does that mean?(官网剖析)(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Apache Kafka™ is a distributed streaming p ...

  7. python 的 reshape强制转换格式的用途

    shu=[[ 0.03046758], [ 0.05485586], [ 0.03282908], [ 0.02107211], [ 0.0391144 ], [ 0.07847244], [ 0.1 ...

  8. ZOJ 2723 Semi-Prime ||ZOJ 2060 Fibonacci Again 水水水!

    两题水题: 1.如果一个数能被分解为两个素数的乘积,则称为Semi-Prime,给你一个数,让你判断是不是Semi-Prime数. 2.定义F(0) = 7, F(1) = 11, F(n) = F( ...

  9. report_timing

    report_timing   -max_path   2 会报告两条路径,但不一定是最差的路径 report_timing  -nworst  2  -max_path   2 会报告两条最差的路径

  10. 【Codeforces Round #299 (Div. 2) A】 Tavas and Nafas

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟题 [代码] #include <bits/stdc++.h> using namespace std; map & ...