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

本文备注:(时间戳单位为毫秒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. docker+rabbitmq的安装

    docker pull rabbitmq:management docker run -d -p : -p : -p : -p : -p : -v /data/rabbitmq-data/:/var/ ...

  2. linux终端使用ss代理

    title: linux终端使用ss代理 date: 2017-11-09 21:06:16 tags: linux categories: linux 系统为archlinux 先将ss代理转化为h ...

  3. swagger 自动生成接口测试用例

    ---整体更新一波--- 1.实际工作中,因为要动手输入的地方比较多,自动生成的异常接口用例感觉用处不大,就先去掉了,只保留了正常的: 2.接口有改动的,如果开发人员没有及时告知或没有详细告知,会增加 ...

  4. python locust 性能测试:locsut参数化-保证并发测试数据唯一性,不循环取数据

    from locust import TaskSet, task, HttpLocustimport queue class UserBehavior(TaskSet): @task def test ...

  5. mysql(5.7以上)查询报错:ORDER BY clause is not in GROUP BY..this is incompatible with sql_mode=only_full_group_by

    执行mysql命令查询时: select * from table_name错误信息如: [Err] 1055 - Expression #1 of ORDER BY clause is not in ...

  6. hdu4916 Count on the path

    调了好久.... •把树视为以1为根的有向树,然后将1删除 •原树变为一个森林,并且任一棵树的根节点均为原树中1的子节点 •只需要考虑最小编号前3小的三棵树 •记f[x][y]为去掉x和y两棵树后的最 ...

  7. JavaScript中的变量提升和函数提升

    在EcmaScript5中只有全局作用域和函数作用域,EcmaScript6增加了块级作用域. 块级作用域(一对花括号{}即为一个块级作用域) 变量提升 console.log(name); //un ...

  8. idea软件快速设置主题颜色

    打开idea,然后在File->setting->在搜索框里面输入theme->然后点击appearance->将theme的主题设置为IntellilJ,然后就可以了,如下图 ...

  9. 关于django1.8版本的静态文件配置

    环境:Python3.5.4,django1.8.1. 在页面使用js时,总是提示404找不到js文件. 于是,看看了settings文件 好像也没什么毛病.导入的方式也换了很多种,总是不行,于是只好 ...

  10. flask --- 02. 路由, 初始化配置,蓝图

    一.Flask 路由 1.添加路由的方式 ① ② 实例: ① @app.route("/my_de") def detail() ② def detail() app.add_ur ...