JavaScript日期字符串和时间戳转换
2017-06-30 11:46
369 查看
//获取当前时间:
var myDate = new Date();//当前时间
var year = myDate.getFullYear();//当前年份
var month = myDate.getMonth() + 1;//当前月份
var day = myDate.getDate();//当前日
myDate.getYear(); //获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-????)
myDate.getMonth(); //获取当前月份(0-11,0代表1月)
myDate.getDate(); //获取当前日(1-31)
myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours(); //获取当前小时数(0-23)
myDate.getMinutes(); //获取当前分钟数(0-59)
myDate.getSeconds(); //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleDateString(); //获取当前日期
var mytime=myDate.toLocaleTimeString(); //获取当前时间
myDate.toLocaleString( ); //获取日期与时间
var oneDay = 1000 * 60 * 60 * 24;
//获取最近一周的日期
var lastDate = new Date(myDate - oneDay * 6);
var lastYear = lastDate.getFullYear();
var lastMonth = lastDate.getMonth() + 1;
var lastDay = lastDate.getDate();
//获取当前月的最后一天
var day = new Date(year ,month , 0);
var lastdate = day.getDate();//当前月的最后一天
//获取最近N个月的日期
var lastDate = new Date(myDate - oneDay * myDate.getDate());
lastDate = new Date(lastDate - N * oneDay * (lastDate.getDate() - 1));
var lastYear = lastDate.getFullYear();
var lastMonth = lastDate.getMonth() + 1;
var lastDay = lastDate.getDate();
//字符串转换为时间戳
var date="2014-05-08 00:22:11";
date = new Date(Date.parse(date.replace(/-/g, "/")));
date = date.getTime();
方法二:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
// 获取当前时间戳(以s为单位)
var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000;
//当前时间戳为:1403149534
console.log("当前时间戳为:" + timestamp);
// 获取某个时间格式的时间戳
var stringTime = "2014-07-10 10:21:12";
var timestamp2 = Date.parse(new Date(stringTime));
timestamp2 = timestamp2 / 1000;
//2014-07-10 10:21:12的时间戳为:1404958872
console.log(stringTime + "的时间戳为:" + timestamp2);
// 将当前时间换成时间格式字符串
var timestamp3 = 1403058804;
var newDate = new Date();
newDate.setTime(timestamp3 * 1000);
// Wed Jun 18 2014
console.log(newDate.toDateString());
// Wed, 18 Jun 2014 02:33:24 GMT
console.log(newDate.toGMTString());
// 2014-06-18T02:33:24.000Z
console.log(newDate.toISOString());
// 2014-06-18T02:33:24.000Z
console.log(newDate.toJSON());
// 2014年6月18日
console.log(newDate.toLocaleDateString());
// 2014年6月18日 上午10:33:24
console.log(newDate.toLocaleString());
// 上午10:33:24
console.log(newDate.toLocaleTimeString());
// Wed Jun 18 2014 10:33:24 GMT+0800 (中国标准时间)
console.log(newDate.toString());
// 10:33:24 GMT+0800 (中国标准时间)
console.log(newDate.toTimeString());
// Wed, 18 Jun 2014 02:33:24 GMT
console.log(newDate.toUTCString());
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 k in date) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1
? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
}
return format;
}
console.log(newDate.format('yyyy-MM-dd h:m:s'));
</script>
JavaScript 获取当前时间戳:
第一种方法:
var timestamp = Date.parse(new Date());
结果:1280977330000
第二种方法:
var timestamp = (new Date()).valueOf();
结果:1280977330748
第三种方法:
var timestamp=new Date().getTime();
结果:1280977330748
第一种:获取的时间戳是把毫秒改成000显示,
第二种和第三种是获取了当前毫秒的时间戳
javascript中Date()构造函数参数:
var theDate=new Date();
theDate.getDate();
使用以上代码可以获取当前日期的天。
new Date()
new Date(milliseconds)
new Date(datestring)
new Date(year,month,day)
new Date(year,month,day,hours)
new Date(year,month,day,hours,minutes)
new Date(year,month,day,hours,minutes,seconds)
new Date(year,month,day,hours,minutes,seconds,microseconds)
下面就对以上几个构造函数进行简单的分析。
1.new Date(),没有参数的时候,创建的是当前时间日期对象。
2.new Date(milliseconds),当参数为数字的时候,那么这个参数就是时间戳,被视为毫秒,创建一个距离1970年1月一日指定毫秒的时间日期对象。
3.new Date(datestring),此参数是一个字符串,并且此字符串一定能够使用Date.parse()转换。
4.以下六个构造函数是精确定义:
1).year,是一个整数,如果是0-99,那么在此基础上加1900,其他的都原样返回。
2).month,是一个整数,范围是0-11。
3.day,是一个整数,范围是1-31。
4.hours,是一个整数,范围是0-23。
5.minutes,是一个整数,范围是0-59。
6.seconds,是一个整数,范围是0-59.
7.microseconds,是一个整数,范围是0-9999。
一.时间日期转换为时间戳:
现在有这么一个时间日期:"2013/5/12 20:10:20",下面将其转换为时间戳形式:
代码如下:
利用getTime()函数即可获取指定时间日期对象的时间戳。
接下来再给大家介绍javascript的Date对象的getTime()方法
getTime()方法的定义和用法:
此方法返回当前时间距1970年1月1日之间的毫秒数。
注:该方法需要使用一个Date对象来调用。
点击可参阅更多相关Date对象方法和属性。
语法结构:
dateObject.getTime()
实例代码:
以上代码可以输出当前时间距1970年1月1日之间的毫秒数。
其实还有其他很多方法,这里就不介绍了,只需要知道此中方式就可以,其他可以自己慢慢积累。
二.时间戳转换为事件日期:
实例代码一:
javascript的Date对象的toLocaleString()方法 toLocaleString()方法的定义和用法:
此方法可根据本地时间把 Date 对象转换为字符串,并返此字符串。
注:此方法需要用Date对象的实例调用。
语法结构:
dateObject.toLocaleString()
实例代码:
说下javascript的Date对象的toLocaleString()方法
toLocaleString()方法的定义和用法:
此方法可根据本地时间把 Date 对象转换为字符串,并返此字符串。
注:此方法需要用Date对象的实例调用。
语法结构:
dateObject.toLocaleString()
实例代码:
实例二:
var myDate = new Date();//当前时间
var year = myDate.getFullYear();//当前年份
var month = myDate.getMonth() + 1;//当前月份
var day = myDate.getDate();//当前日
myDate.getYear(); //获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-????)
myDate.getMonth(); //获取当前月份(0-11,0代表1月)
myDate.getDate(); //获取当前日(1-31)
myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours(); //获取当前小时数(0-23)
myDate.getMinutes(); //获取当前分钟数(0-59)
myDate.getSeconds(); //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleDateString(); //获取当前日期
var mytime=myDate.toLocaleTimeString(); //获取当前时间
myDate.toLocaleString( ); //获取日期与时间
var oneDay = 1000 * 60 * 60 * 24;
//获取最近一周的日期
var lastDate = new Date(myDate - oneDay * 6);
var lastYear = lastDate.getFullYear();
var lastMonth = lastDate.getMonth() + 1;
var lastDay = lastDate.getDate();
//获取当前月的最后一天
var day = new Date(year ,month , 0);
var lastdate = day.getDate();//当前月的最后一天
//获取最近N个月的日期
var lastDate = new Date(myDate - oneDay * myDate.getDate());
lastDate = new Date(lastDate - N * oneDay * (lastDate.getDate() - 1));
var lastYear = lastDate.getFullYear();
var lastMonth = lastDate.getMonth() + 1;
var lastDay = lastDate.getDate();
//字符串转换为时间戳
var date="2014-05-08 00:22:11";
date = new Date(Date.parse(date.replace(/-/g, "/")));
date = date.getTime();
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript"> // 获取当前时间戳(以s为单位) var timestamp = Date.parse(new Date()); timestamp = timestamp / 1000; //当前时间戳为:1403149534 console.log("当前时间戳为:" + timestamp); // 获取某个时间格式的时间戳 var stringTime = "2014-07-10 10:21:12"; var timestamp2 = Date.parse(new Date(stringTime)); timestamp2 = timestamp2 / 1000; //2014-07-10 10:21:12的时间戳为:1404958872 console.log(stringTime + "的时间戳为:" + timestamp2); // 将当前时间换成时间格式字符串 var timestamp3 = 1403058804; var newDate = new Date(); newDate.setTime(timestamp3 * 1000); // Wed Jun 18 2014 console.log(newDate.toDateString()); // Wed, 18 Jun 2014 02:33:24 GMT console.log(newDate.toGMTString()); // 2014-06-18T02:33:24.000Z console.log(newDate.toISOString()); // 2014-06-18T02:33:24.000Z console.log(newDate.toJSON()); // 2014年6月18日 console.log(newDate.toLocaleDateString()); // 2014年6月18日 上午10:33:24 console.log(newDate.toLocaleString()); // 上午10:33:24 console.log(newDate.toLocaleTimeString()); // Wed Jun 18 2014 10:33:24 GMT+0800 (中国标准时间) console.log(newDate.toString()); // 10:33:24 GMT+0800 (中国标准时间) console.log(newDate.toTimeString()); // Wed, 18 Jun 2014 02:33:24 GMT console.log(newDate.toUTCString()); 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 k in date) { if (new RegExp("(" + k + ")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length)); } } return format; } console.log(newDate.format('yyyy-MM-dd h:m:s')); </script>
方法二:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
// 获取当前时间戳(以s为单位)
var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000;
//当前时间戳为:1403149534
console.log("当前时间戳为:" + timestamp);
// 获取某个时间格式的时间戳
var stringTime = "2014-07-10 10:21:12";
var timestamp2 = Date.parse(new Date(stringTime));
timestamp2 = timestamp2 / 1000;
//2014-07-10 10:21:12的时间戳为:1404958872
console.log(stringTime + "的时间戳为:" + timestamp2);
// 将当前时间换成时间格式字符串
var timestamp3 = 1403058804;
var newDate = new Date();
newDate.setTime(timestamp3 * 1000);
// Wed Jun 18 2014
console.log(newDate.toDateString());
// Wed, 18 Jun 2014 02:33:24 GMT
console.log(newDate.toGMTString());
// 2014-06-18T02:33:24.000Z
console.log(newDate.toISOString());
// 2014-06-18T02:33:24.000Z
console.log(newDate.toJSON());
// 2014年6月18日
console.log(newDate.toLocaleDateString());
// 2014年6月18日 上午10:33:24
console.log(newDate.toLocaleString());
// 上午10:33:24
console.log(newDate.toLocaleTimeString());
// Wed Jun 18 2014 10:33:24 GMT+0800 (中国标准时间)
console.log(newDate.toString());
// 10:33:24 GMT+0800 (中国标准时间)
console.log(newDate.toTimeString());
// Wed, 18 Jun 2014 02:33:24 GMT
console.log(newDate.toUTCString());
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 k in date) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1
? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
}
return format;
}
console.log(newDate.format('yyyy-MM-dd h:m:s'));
</script>
JavaScript 获取当前时间戳:
第一种方法:
var timestamp = Date.parse(new Date());
结果:1280977330000
第二种方法:
var timestamp = (new Date()).valueOf();
结果:1280977330748
第三种方法:
var timestamp=new Date().getTime();
结果:1280977330748
第一种:获取的时间戳是把毫秒改成000显示,
第二种和第三种是获取了当前毫秒的时间戳
javascript中Date()构造函数参数:
var theDate=new Date();
theDate.getDate();
使用以上代码可以获取当前日期的天。
new Date()
new Date(milliseconds)
new Date(datestring)
new Date(year,month,day)
new Date(year,month,day,hours)
new Date(year,month,day,hours,minutes)
new Date(year,month,day,hours,minutes,seconds)
new Date(year,month,day,hours,minutes,seconds,microseconds)
下面就对以上几个构造函数进行简单的分析。
1.new Date(),没有参数的时候,创建的是当前时间日期对象。
2.new Date(milliseconds),当参数为数字的时候,那么这个参数就是时间戳,被视为毫秒,创建一个距离1970年1月一日指定毫秒的时间日期对象。
3.new Date(datestring),此参数是一个字符串,并且此字符串一定能够使用Date.parse()转换。
4.以下六个构造函数是精确定义:
1).year,是一个整数,如果是0-99,那么在此基础上加1900,其他的都原样返回。
2).month,是一个整数,范围是0-11。
3.day,是一个整数,范围是1-31。
4.hours,是一个整数,范围是0-23。
5.minutes,是一个整数,范围是0-59。
6.seconds,是一个整数,范围是0-59.
7.microseconds,是一个整数,范围是0-9999。
现在有这么一个时间日期:"2013/5/12 20:10:20",下面将其转换为时间戳形式:
代码如下:
接下来再给大家介绍javascript的Date对象的getTime()方法
getTime()方法的定义和用法:
此方法返回当前时间距1970年1月1日之间的毫秒数。
注:该方法需要使用一个Date对象来调用。
点击可参阅更多相关Date对象方法和属性。
语法结构:
dateObject.getTime()
实例代码:
其实还有其他很多方法,这里就不介绍了,只需要知道此中方式就可以,其他可以自己慢慢积累。
二.时间戳转换为事件日期:
实例代码一:
此方法可根据本地时间把 Date 对象转换为字符串,并返此字符串。
注:此方法需要用Date对象的实例调用。
语法结构:
dateObject.toLocaleString()
实例代码:
toLocaleString()方法的定义和用法:
此方法可根据本地时间把 Date 对象转换为字符串,并返此字符串。
注:此方法需要用Date对象的实例调用。
语法结构:
dateObject.toLocaleString()
实例代码:
相关文章推荐
- javascript时间戳和日期字符串相互转换
- javascript时间戳和日期字符串相互转换
- javascript时间戳和日期字符串相互转换(json)
- javascript转换日期字符串---转化为Date对象---再转化为时间戳
- javascript时间戳和日期字符串相互转换
- javascript时间戳和日期字符串相互转换
- javascript时间戳和日期字符串相互转换
- javascript时间戳和日期字符串相互转换
- javascript时间戳和日期字符串相互转换
- javascript时间戳和日期字符串相互转换
- javascript时间戳和日期字符串相互转换代码(超简单)
- javascript时间戳和日期字符串相互转换代码(超简单)
- javascript时间戳和日期字符串相互转换
- javascript时间戳和日期字符串相互转换
- javascript时间戳和日期字符串相互转换
- javascript时间戳和日期字符串相互转换
- javascript时间戳和日期字符串相互转换
- javascript时间戳和日期字符串相互转换
- javascript时间戳和日期字符串相互转换