js时间和时间戳之间如何转换(汇总)
js时间和时间戳之间如何转换(汇总)
一、总结
一句话总结:
1、js中通过new Date()来获取时间对象,
2、这个时间对象可以通过getTime()方法获取时间戳,
3、也可以通过getYear()、getMonth()获取年月,
4、也可以通过toTimeString().substr(0, 8));的方法获取时分秒。
1、js中怎么获取日期对象?
2、js中如何将日期对象转化为时间戳?(四种方法)
valueOf()函数返回指定对象的原始值获得准确的时间戳值(new Date()).valueOf(); d、通过Date.parse方法Date.parse(new Date());不推荐这种办法,毫秒级别的数值被转化为0003、js中如何将时间戳转化为时间?
4、js中如何获取一个时间对象的时分秒?
5、时间对象的toLocaleDateString()方法是干嘛的?
6、js中如何通过时间对象获取年?
7、js中如何通过时间对象获取月?
8、js中如何通过时间对象获取日?
9、js中获取时间对象的年月日方法的前缀是什么?
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中怎么获取日期对象?
2、js中如何将日期对象转化为时间戳?(四种方法)
valueOf()函数返回指定对象的原始值获得准确的时间戳值(new Date()).valueOf(); d、通过Date.parse方法Date.parse(new Date());不推荐这种办法,毫秒级别的数值被转化为0003、js中如何将时间戳转化为时间?
4、js中如何获取一个时间对象的时分秒?
5、时间对象的toLocaleDateString()方法是干嘛的?
6、js中如何通过时间对象获取年?
7、js中如何通过时间对象获取月?
8、js中如何通过时间对象获取日?
9、js中获取时间对象的年月日方法的前缀是什么?
10、js时间对象中的time是什么?
js时间和时间戳之间如何转换(汇总)的更多相关文章
- js时间与时间戳之间的转换操作,返回天、小时、分,全家桶
1.将时间戳转换成时间 var formatDate = function(d) { var now = new Date(d); var year = now.getFullYear(); var ...
- python—时间与时间戳之间的转换
python-时间与时间戳之间的转换 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块 ...
- 【python-时间戳】时间与时间戳之间的转换
对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块,具体的操作有如下的几种: 将时间转换 ...
- python 时间与时间戳之间的转换
https://blog.csdn.net/kl28978113/article/details/79271518 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运 ...
- python——时间与时间戳之间的转换
http://blog.csdn.net/google19890102/article/details/51355282
- JS 时间字符串与时间戳之间的转换
1.当前时间换时间戳 var timestamp = parseInt(new Date().getTime()/1000); // 当前时间戳 document.write(timestamp); ...
- Python时间、日期、时间戳之间的转换
一.字符串与为时间字符串之间的互相转换 方法:time模块下的strptime方法 a = "2012-11-11 23:40:00" # 字符串转换为时间字符串 import t ...
- js 时间与时间戳的转换
一:时间转时间戳:javascript获得时间戳的方法有四种,都是通过实例化时间对象 new Date() 来进一步获取当前的时间戳 1.var timestamp1 = Date.parse(n ...
- python 时间字符串和时间戳之间的转换
https://blog.csdn.net/qq_37193537/article/details/78987949 1.将字符串的时间转换为时间戳 方法: a = " ...
随机推荐
- 洛谷——P1781 宇宙总统
https://www.luogu.org/problem/show?pid=1781 题目背景 宇宙总统竞选 题目描述 地球历公元6036年,全宇宙准备竞选一个最贤能的人当总统,共有n个非凡拔尖的人 ...
- [Vue + TS] Create your own Decorators in Vue with TypeScript
We’ve used @Watch, @Inject and more decorators from vue-property-decorator. In this lesson however w ...
- Android提示版本号更新操作流程
Android提示版本号更新操作流程 2014年5月8日: andorid的app应用中都会有版本号更新的操作,今天空暇的时候就花了点心思弄了一下.主要技术方面用到了AsyncTask异步载入.htt ...
- How to remove a Data Guard Configuration from Primary Database (文档 ID 733794.1)
APPLIES TO: Oracle Database - Enterprise Edition - Version 10.1.0.2 to 11.2.0.3 [Release 10.1 to 11. ...
- 软件——protel 的pcb电路图制作
近期一直在学习PCB板的绘制.
- Swiper+ ejs模板引擎+ iScroll插件知识总结
一. Swiper swiper是一个应用于移动端的动画插件,原理类似于轮播图 官网 http://www.swiper.com.cn/# html结构 <div class="swi ...
- UVA 11437 - Triangle Fun 向量几何
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- [Debug] Use Snippets to Store Behaviors in Chrome DevTools
Using the Snippets tab in the source devtool you can define and run arbitrary pieces of code in your ...
- 为什么一款优秀的移动工具类应用必须开发PCclient?
移动大潮气势汹汹,PC端似乎已经一条腿跨进了坟墓. 作为一个windows开发者.难免有些焦灼. windows真的已死吗?真的无用武之地了吗? 或许是人云亦云吧. 突然看到了这篇文章,感触颇深. 文 ...
- php实现兼容Unicode文字的字符串大写和小写转换strtolower()和strtoupper()
前言 网上流传着这么一个腾讯笔试题: PHP的strtolower()和strtoupper()函数在安装非中文系统的server下可能会导致将汉字转换为乱码,请写两个替代的函数实现兼容Unicode ...