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

本文备注:(时间戳单位为毫秒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也是位~~

完毕哟~

最新文章

  1. 用JS做一个简单的电商产品放大镜功能
  2. 各种UIButton
  3. Oracle sqlplus设置显示格式命令详解
  4. Java程序设计 实验三
  5. WPF中的VisualTreeHelper
  6. JS对于Android和IOS平台的点击响应的适配
  7. windows下计算文件的md和sha值
  8. ASP.NET不通过添加web引用的方式调用web service接口
  9. html postMessage 创建聊天应用
  10. 2016中国大学生程序设计竞赛 - 网络选拔赛 1001 A water problem (大数取余)
  11. 利用GPU实现大规模动画角色的渲染
  12. 整理C++面试题for非CS程序猿——更新至【48】
  13. Ubuntu18.04LTS安装Nvidia显卡
  14. 洛谷评测机BUG(应该是)
  15. 使用FileZilla从Linux系统下载文件
  16. mysql覆盖索引详解
  17. JDBC连接各种数据库的方法,连接MySql,Oracle数据库
  18. centos 安装nginx笔记
  19. odoo开发笔记-自定义发送邮件模板
  20. java反射究竟消耗多少效率

热门文章

  1. bugfree3.0.1-邮件配置
  2. CDN和智能DNS原理和应用 (原)
  3. 无需激活直接同步登入discuz,php代码(直接可用)
  4. Java IO--NIO(二)
  5. centos7安装redist 以及redis扩展
  6. scrapy流程图
  7. 0005-20180422-自动化第六章-python基础学习笔记
  8. Tomcat 加载 jsp 异常:ServletException: java.lang.LinkageError
  9. 史上最全python面试题详解(一)(附带详细答案(持续更新))
  10. C++ 打印机设置