js时间戳如何转时间
js时间戳如何转时间
一、总结
一句话总结:Date对象分别获取年now.getFullYear()月now.getMonth()+1日now.getDate()即可
Date对象分别获取年now.getFullYear()月now.getMonth()+1日now.getDate()即可
二、js 时间与时间戳的转换
参考:js 时间与时间戳的转换 - LD_Joy - 博客园
https://www.cnblogs.com/nield-bky/p/6040853.html
一:时间转时间戳:
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);
}
三、js常用日期函数
//1、获取时间戳对应的时间,格式YYYY-MM-DD
function getTsFormatDate(timeStamp) {
var date = new Date(timeStamp);
//console.log(date); 结果为:Tue Apr 02 2019 07:49:23 GMT+0800 (中国标准时间)
var seperator1 = "-";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = year + seperator1 + month + seperator1 + strDate;
return currentdate;
}
//2、获取现在对应的时间,格式YYYY-MM-DD
function getNowFormatDate() {
var timeStamp = new Date().getTime();
return getTsFormatDate(timeStamp);
} //3、获取指定日期对应的时间,格式YYYY-MM-DD,比如一天前,参数则为-1,二天后,参数则为2
function getSpecialFormatDate(dayOffset){
var timeStamp = new Date().getTime()+dayOffset*1000*24*60*60;
return getTsFormatDate(timeStamp);
}
js时间戳如何转时间的更多相关文章
- JS时间戳格式化日期时间
由于mysql数据库里面存储时间存的是时间戳,取出来之后,JS要格式化一下显示.(李昌辉) 用的次数比较多,所以写了一个简单方法来转换: //时间戳转时间 function RiQi(sj) { va ...
- JS时间戳格式化日期时间 由于mysql数据库里面存储时间存的是时间戳,取出来之后,JS要格式化一下显示。
//时间戳转时间 function RiQi(sj) { var now = new Date(sj*1000); var year=now.getFullYear(); var month=now. ...
- js 时间戳转换成时间格式,可自定义格式
由于 c# 通过ajax获取的时间 传到前台 格式为:/Date(1354116249000)/ 所以需要转换一下,想要什么格式 更改 format() 里的 返回语句 就可以了 formatDate ...
- js 时间戳转化为时间
// 时间戳转为时间 bb="xxxx"://时间戳 var date = new Date(bb); Y = date.getFullYear() + '-'; M = (dat ...
- js 时间戳 和 格式化时间转化
function timestampToTime(timestamp) { var date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的 ...
- JS时间戳转换成时间格式
TimeNow: function (val) { var date = new Date(val); var Y = date.getFullYear(); var m = date.getMont ...
- JS时间戳比较大小:对于一组时间戳(开始时间~结束时间)和另一组时间戳进行比较,用于判断被比较时间戳组是否在要求范围内
/* *JS时间戳比较大小:对于一组时间戳(开始时间~结束时间)和另一组时间戳进行比较,用于判断被比较时间戳组是否在要求范围内 *@param date1 date2(形如:'2015-01-01'类 ...
- js中时间戳转换成时间格式
js中时间戳转换成时间格式, // 时间戳转换成时间格式 var formatDate = function(date){ date = new Date(date); var y=date.getF ...
- js笔记系列之--时间及时间戳
js入门系列之 时间及时间戳 时间及时间戳 时间及时间戳是js里面很常见的一个概念,在我们写前端页面的时候,经常会遇到需要获取当前时间的情况,所以,了解js中的时间概念非常重要.而时间戳是指格林威治时 ...
随机推荐
- Win7的话,可能有十种简单的方法进行提速呢
1.窗口转换更快速 Windows7绚丽的效果的确美观,但漂亮的效果就需要拿速度来交换,因此如果你想要Windows7中的各个窗口切换得更快速,那关闭窗口最大.最小化的动画效果后,你会发现窗口切换得更 ...
- linux /Android 平台下使用 i2c-tools
下载源码将 i2c-tools 代码下载到 Android 源码的 external 目录下 在 i2c-tools 目录下新建 Android.mk 文件,内容如下: # external/i2c- ...
- Python中的对象行为与特殊方法(一)对象的创建与销毁
Python中类调用__new__()类方法来创建实例,调用__init__()方法来初始化对象,对象的销毁则调用__del__()方法. __new__()方法第一个参数为类cls,通常返回cls的 ...
- Windows Installation
If you're running on Windows it is good to run Jenkins as a service so it starts up automatically wi ...
- CF 1087解题报告
cf解题报告 记录一下吧 做出:T1 rating :-97 想起几个月前做不出T1还是有点小搞笑呀2333 T1 双指针+特判 T2 发现k特别小,枚举剩余系 还要判断是否是能被n整除 移项发现可以 ...
- 【解决办法】Undefined command/function 'mapminmax'.
原因: 低版本7.0中没有mapminmax这个函数,对应的归一化函数是premnmx和postmnmx,具体请查看着两个函数的用法升级到2009就肯定可以正常使用这个函数了 解释: premnmx. ...
- 【基本知识】Flume基本环境搭建以及原理
系统:CentOS6.5JDK:1.8.0_144Flume:flume-ng-1.6.0-cdh5.12.0 一.什么是Flume flume 作为 cloudera 开发的实时日志收集系统,受到了 ...
- centos6.5下安装jdk并配置环境变量
链接: https://blog.csdn.net/wawawawawawaa/article/details/81158943 以下链接供参考: https://blog.csdn.net/Bugg ...
- 论操作系统的IO
论事件驱动与异步IO - 简书 https://www.jianshu.com/p/814c7e7c4647
- How to resize slide dimensions without resizing any objects on the slide?
IF you are competent to unzip the pptx file and modify the XML it can be done, the slide size will c ...