1、获取年、月、日和将时间戳转换成日期格式
// 简单的一句代码
var date = new Date(时间戳); //获取一个时间对象
/**
1. 下面是获取时间日期的方法,需要什么样的格式自己拼接起来就好了
2. 更多好用的方法可以在这查到 -> http://www.w3school.com.cn/jsref/jsref_obj_date.asp
*/
date.getFullYear(); // 获取完整的年份(4位,)
date.getMonth(); // 获取月份(,0代表1月,用的时候记得加上1)
date.getDate(); // 获取日()
date.getTime(); // 获取时间(从开始的毫秒数)
date.getHours(); // 获取小时数()
date.getMinutes(); // 获取分钟数()
date.getSeconds(); // 获取秒数()
// 比如需要这样的格式 yyyy-MM-dd hh:mm:ss
var date = new Date);
Y = date.getFullYear() + -;
M = (date.getMonth()+1 < ? 0+(date.getMonth()+1) : date.getMonth()+1) + -;
D = date.getDate() + ;
h = date.getHours() + :;
m = date.getMinutes() + :;
s = date.getSeconds();
console.log(Y+M+D+h+m+s);
// 输出结果: ::
2、将日期格式转换成时间戳
// 也很简单
var strtime = :::;
var date = new Date(strtime); //传入一个时间格式,如果不传入就是获取现在的时间了,这样做不兼容火狐。
// 可以这样做
var date = new Date(strtime.replace(/-/g, /));
// 有三种方式获取,在后面会讲到三种方式的区别
time1 = date.getTime();
time2 = date.valueOf();
time3 = Date.parse(date);
/*
三种获取的区别:
第一、第二种:会精确到毫秒
第三种:只能精确到秒,毫秒将用0来代替
比如上面代码输出的结果(一眼就能看出区别):
*/
3、Date()参数常用的形式有以下几种
new Date(yyyy/MM/dd hh:mm:ss);
new Date(yyyy/MM/dd);
new Date(yyyy,mth,dd);
比如:
new Date(//::);
new Date(//);
new Date,8,);
作者:小破孩呦
来源:CSDN
原文:https://blog.csdn.net/qq_38543537/article/details/
版权声明:本文为博主原创文章,转载请附上博文链接!
获取当前时间日期格式如“ ::”
function getNowFormatDate() {
var date = new Date();
var seperator1 = -;
var seperator2 = :;
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = 0 + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = 0 + strDate;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
+ + date.getHours() + seperator2 + date.getMinutes()
+ seperator2 + date.getSeconds();
return currentdate;
} 