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. 洛谷——P1443 马的遍历

    https://www.luogu.org/problem/show?pid=1443#sub 题目描述 有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达 ...

  2. 19. Spring Boot Shiro 权限管理

    转自:https://blog.csdn.net/catoop/article/details/50520958

  3. ORACLE10g R2【RAC+ASM→单实例FS】

    ORACLE10g R2[RAC+ASM→单实例FS] 10g R2 RAC+ASMà单实例FS的DG,建议禁用OMF. 本演示案例所用环境:   primary standby OS Hostnam ...

  4. 微信小程序实现运动步数排行(可删除)

    wxml <!-- 向左滑动删除功能 --> <view class="item-box"> <view class="items" ...

  5. NSDate时间

    NSDate 使用 ios时间的秒数 取当前时间的秒数 NSTimeInterval time = [[NSDate date] timeIntervalSince1970]; long long i ...

  6. [React Intl] Format a Date Relative to the Current Date Using react-intl FormattedRelative

    Given a date, we’ll use the react-intl FormattedRelative component to render a date in a human reada ...

  7. PHP URL参数获取方式的四种例子

    在已知URL参数的情况下,我们可以根据自身情况采用$_GET来获取相应的参数信息($_GET['name']);那,在未知情况下如何获取到URL上的参数信息呢? 第一种.利用$_SERVER内置数组变 ...

  8. 9.9 Binder系统_Java实现_Android里java程序的编译启动

    如果知道了进程号:通过ls /proc/进程号/task 可以看到所有线程    cat /proc/进程号/task/线程号/comm  可以达到线程名字(主线程是main,主线程号就是进程号) d ...

  9. ArcEngine数据删除几种方法和性能比较

    转自原文 ArcEngine数据删除几种方法和性能比较 一.  几种删除方法代码 1.  查询结果中删除 private void Delete1(IFeatureClass PFeatureclas ...

  10. [Nuxt] Update Vuex State with Mutations and MapMutations in Vue.js

    You commit changes to state in Vuex using defined mutations. You can easily access these state mutat ...