跟后台对接的时候经常碰到时间格式的问题,有时返回的是时间戳,有时返回的是具体时间,需求又需要它们之间的转换,所以干脆把之前遇到过的情况都给记录下来,以供自己参考!

本文备注:(时间戳单位为毫秒ms,换算秒s需timestrap/1000;
例子使用时间为:'2017/5/11 11:42:18')

1.获取当前日期的时间戳

  var timestrap=Date.parse(new Date());
//或者
var timestrap=(new Date()).getTime();
console.log(timestrap);//1494474138000

  我经常用的是(new Date()).getTime()这种方式,很少用Date.parse(new Date()),因为会转换后三位毫秒数,不精确。

2.获取具体时间格式的时间戳

  var timestrap=(new Date('2017/5/11 11:42:18')).getTime();
//

3.转换指定时间戳

  var time=new Date(1494474138000);
//Thu May 11 2017 11:42:18 GMT+0800 (中国标准时间)
//Thu May 11 2017 11:42:18 GMT+0800 (CST)

4. Date()

  定义:

  var timestrap=1494474138000;
var newDate=new Date();
newDate.setTime(timestrap);
  Date()实例具体使用情况:
  console.log(newDate.toLocaleString());//2017/5/11 上午11:42:18
console.log(newDate.toString()); //Thu May 11 2017 11:42:18 GMT+0800 (中国标准时间)
console.log(newDate.toDateString());//Thu May 11 2017
console.log(newDate.toTimeString());//11:42:18 GMT+0800 (中国标准时间)
console.log(newDate.toGMTString()); //Thu, 11 May 2017 03:42:18 GMT
console.log(newDate.toUTCString()); //Thu, 11 May 2017 03:42:18 GMT
console.log(newDate.toISOString()); //2017-05-11T03:42:18.000Z
console.log(newDate.toJSON()); //2017-05-11T03:42:18.000Z
  //返回一个指定日期对象的年份;
console.log(newDate.getFullYear()); //2017,年
  //返回一个指定的日期对象的月份,返回一个0 到 11的整数值, 0 代表一月,1 代表二月,...
console.log(newDate.getMonth()); //4,月
  //返回一个指定的日期对象为一个月中的第几天;
console.log(newDate.getDate()); //11,日
  //返回一个指定的日期对象的小时,返回一个0 到 23之间的整数值;
console.log(newDate.getHours()); //11,时
 //返回一个指定的日期对象的分钟数,返回一个0 到 59的整数值;
console.log(newDate.getMinutes()); //42,分
  //返回一个指定的日期对象的秒数,返回一个 0 到 59 的整数值;
console.log(newDate.getSeconds()); //18,秒
 //返回一个指定的日期对象的毫秒数,返回一个0 到 999的整数;
console.log(newDate.getMilliseconds());//0,毫秒
  //返回一个具体日期中一周的第几天,0 表示星期天,1 表示星期一,...
console.log(newDate.getDay()); //4,星期
  //返回值一个数值,表示从1970年1月1日0时0分0秒(UTC,即协调世界时)距离该日期对象所代表时间的毫秒数;
console.log(newDate.getTime()); //1494474138000
方法使用情况:
//解析一个表示日期的字符串,返回毫秒数
Date.parse('2017/5/11 11:42:18');//1494474138000
  //返回自 1970-1-1 00:00:00  UTC (时间标准时间)至今所经过的毫秒数,类型为Number;
Date.now(); //时间戳

5.利用原型对象prototype来做时间格式的转换

"M+": '月份',
"d+": '日',
"h+": '小时',
"m+": '分钟',
"s+": '秒',
"q+": '季度',
"S+": '毫秒',
"y+": '年份',
   Date.prototype.format=function(format){
var date={
"M+": this.getMonth()+1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth()+3)/3),
"S+": this.getMilliseconds(),
};
if(/(y+)/i.test(format)){
format=format.replace(RegExp.$1,(this.getFullYear()+'').substr(4-RegExp.$1.length));
}
for(var i in date){
if(new RegExp("("+i+")").test(format)){
format=format.replace(RegExp.$1, RegExp.$1.length==1?date[i]:("00"+date[i]).substr((""+date[i]).length));
}
}
return format;
}

  使用用例:

 (new Date('2017-03-23 17:33:45')).format('yyyy/MM/dd h:m:s');
 (new Date()).format('MM,qq');

~~~~~~~~~~~~~~~~~~~~~~~~~ 分割线der ~~~~~~~~~~~~~~~~~~~~~~~~~

2018-11-14补充:

近期和php后台对接时,发现输出的时间戳为位,JS需要换成位的时间戳才能成功换算日期;

JAVA也是位~~

完毕哟~

Javascript时间戳和日期时间的相互转换的更多相关文章

  1. javascript时间戳与日期格式的相互转换

    这里总结下JavaScript中时间戳和日期格式的相互转换方法(自定义函数). 将时间戳转换为日期格式 function timestampToTime(timestamp) { var date = ...

  2. 转:javascript时间戳和日期字符串相互转换

    转:javascript时间戳和日期字符串相互转换 <html xmlns="http://www.w3.org/1999/xhtml"> <head> & ...

  3. js时间戳与日期格式的相互转换

    下面总结一下js中时间戳与日期格式的相互转换: 1. 将时间戳转换成日期格式: function timestampToTime(timestamp) { var date = new Date(ti ...

  4. Unix时间戳转日期时间格式,C#、Java、Python各语言实现!

    之前有个Q上好友没事问我,怎么自己写Unix时间戳转日期时间?于是我就顺手写了个C#版本给他!最近想起来,就萌发多写几个语言的版本分享,权当练习思路外加熟悉另外两种语言. 先说转换步骤 先处理年份,从 ...

  5. js中时间戳与日期时间之间的相互转换

    1.时间戳转换为标准日期时间格式: function timeFormat(dateStr) { var date = new Date(dateStr); Y = date.getFullYear( ...

  6. javascript时间戳与日期格式之间的互转

    1. 将时间戳转换成日期格式 // 简单的一句代码 var date = new Date(时间戳); //获取一个时间对象 /** 1. 下面是获取时间日期的方法,需要什么样的格式自己拼接起来就好了 ...

  7. JavaScript中的日期时间函数

    1.Date对象具有多种构造函数,下面简单列举如下 new Date() new Date(milliseconds) new Date(datestring) new Date(year, mont ...

  8. jquery 时间戳和日期时间转化

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  9. mysql 将时间戳与日期时间的转换

    from_unixtime()是MySQL里的时间函数 mysql>SELECT FROM_UNIXTIME( 1249488000, '%Y%m%d' )  ->20071120 mys ...

随机推荐

  1. SharePoint 命令行

    网站集备份: Backup-SPSite http://sp2013 -Path C:\sp.bak 网站集还原: Restore-SPSite http://sp2013/sites/dyzx -P ...

  2. PHP中new self()和new static()的区别探究

    1.new static()是在PHP5.3版本中引入的新特性. 2.无论是new static()还是new self(),都是new了一个新的对象. 3.这两个方法new出来的对象有什么区别呢,说 ...

  3. Java 基础 IO流(转换流,缓冲)

    一,前言 在学习字符流(FileReader.FileWriter)的时候,其中说如果需要指定编码和缓冲区大小时,可以在字节流的基础上,构造一个InputStreamReader或者OutputStr ...

  4. Python基础(八) yaml在python中的使用

    yaml 通常用来存储数据,类似于json YAML 简介 YAML(Yet Another Markup Language),一种直观的能够被电脑识别的数据序列化格式,是一个可读性高并且容易被人类阅 ...

  5. Gitlab安装以及汉化

    Gitlab安装以及汉化 系统环境: CentOS 7.5 IP:192.168.1.2 关闭selinux.firewalld gitlab-ce-10.8.4 rpm包:下载地址 一.下载并安装g ...

  6. C#-----字节数组(byte[])和字符串相互转换

       Encoding类  表示字符编码 1.字符串转换成字节数组byte[] using System; using System.Collections.Generic; using System ...

  7. 改写pipeline

    为什么要改写方法:get_media_requests,他们的区别在哪里 def get_media_requests(self, item, info):#原始的 return [Request(x ...

  8. vue用npm安装删除模块element-ui mint-ui

    vue用npm安装删除模块element-ui mint-ui 在vue项目中先引入了element-ui,后来发现移动版的需要用mint-ui,所以需要先卸载了再安装.卸载element-ui:np ...

  9. 教你如何在win7中安装cygwin64

    首先,说说我们为什么要安装cygwin吧,长期在win7下开发的人员可能不习惯使用unix系统,但由于工作问题,你又被逼要在unix环境下开发,那该如何是好啊?但现在你不用再纠结了,因为有cygwin ...

  10. jmeter如何玩?

    ApacheJMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试但后来扩展到其他测试领域. 它可以用于测试静态和动态资源例如静态文件.J ...