Javascript时间戳和日期时间的相互转换
跟后台对接的时候经常碰到时间格式的问题,有时返回的是时间戳,有时返回的是具体时间,需求又需要它们之间的转换,所以干脆把之前遇到过的情况都给记录下来,以供自己参考!
本文备注:(时间戳单位为毫秒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时间戳和日期时间的相互转换的更多相关文章
- javascript时间戳与日期格式的相互转换
这里总结下JavaScript中时间戳和日期格式的相互转换方法(自定义函数). 将时间戳转换为日期格式 function timestampToTime(timestamp) { var date = ...
- 转:javascript时间戳和日期字符串相互转换
转:javascript时间戳和日期字符串相互转换 <html xmlns="http://www.w3.org/1999/xhtml"> <head> & ...
- js时间戳与日期格式的相互转换
下面总结一下js中时间戳与日期格式的相互转换: 1. 将时间戳转换成日期格式: function timestampToTime(timestamp) { var date = new Date(ti ...
- Unix时间戳转日期时间格式,C#、Java、Python各语言实现!
之前有个Q上好友没事问我,怎么自己写Unix时间戳转日期时间?于是我就顺手写了个C#版本给他!最近想起来,就萌发多写几个语言的版本分享,权当练习思路外加熟悉另外两种语言. 先说转换步骤 先处理年份,从 ...
- js中时间戳与日期时间之间的相互转换
1.时间戳转换为标准日期时间格式: function timeFormat(dateStr) { var date = new Date(dateStr); Y = date.getFullYear( ...
- javascript时间戳与日期格式之间的互转
1. 将时间戳转换成日期格式 // 简单的一句代码 var date = new Date(时间戳); //获取一个时间对象 /** 1. 下面是获取时间日期的方法,需要什么样的格式自己拼接起来就好了 ...
- JavaScript中的日期时间函数
1.Date对象具有多种构造函数,下面简单列举如下 new Date() new Date(milliseconds) new Date(datestring) new Date(year, mont ...
- 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 ...
- mysql 将时间戳与日期时间的转换
from_unixtime()是MySQL里的时间函数 mysql>SELECT FROM_UNIXTIME( 1249488000, '%Y%m%d' ) ->20071120 mys ...
随机推荐
- vue中router使用keep-alive缓存页面的注意事项
<keep-alive exclude="QRCode"> <router-view></router-view> </keep-aliv ...
- linux系统644、755、777权限详解
在linux系统中644.755.777三种权限是非常重要的一些权限了,下面我来详细的介绍644.755.777三种权限的使用,希望对各位有帮助. 常用的linux文件权限:444 r--r--r-- ...
- JDBC-DAO层数据访问工具类的实现
private static PreparedStatement pst; private static ResultSet rst; public static <T> int inse ...
- db2常见错误代码及原因
sqlcode sqlstate 说明000 00000 SQL语句成功完成01xxx SQL语句成功完成,但是有警告+012 01545 未限定的列名被解释为一个有相互关系的引用+098 01568 ...
- phpcms首页替换
大图轮播替换 {pc:content action="lists" catid="13" order="id DESC" num=" ...
- UI框架搭建DAY2
今天的主要任务是完善NormalPanel, 搭建PopupPanel. 在编写PanelManager的过程中,发现了一个bug.昨天把panelPath直接传给了ResourceManager.G ...
- FractionallySizedBox
import 'package:flutter/material.dart'; void main() { runApp(MaterialApp(home: new MyApp())); } clas ...
- P2472 [SCOI2007]蜥蜴(网络流)
P2472 [SCOI2007]蜥蜴 把每个点拆成2个点,两点之间连边的边权为石柱高度 新建虚拟源点$S$和汇点$T$ $S$向所有有蜥蜴的点连边,边权1 其他边都连$inf$ 剩下就是裸的$dini ...
- Python3 批量替换文本内容
Python3 批量替换文本内容 示例: # coding:utf8 import os; def reset(): i = 0 path = r"H:\asDemo\workdemo\aw ...
- Linux NFS挂载
Linux NFS挂载 一.NFS挂载 192.25.10.101/home/sharedata/azkaban/ODS_HS08 挂载到 192.25.10.102/home/data_azkaba ...