this.startdate=(new Date()).Format("yyyy-MM-dd") //今天的时间 2024-03-07
var timestamp = new Date().getTime(); //时间戳 精确到毫秒
this.enddate=(new Date(timestamp+3*24*60*60*1000)).Format("yyyy-MM-dd") //3天后的年月日 2024-03-10
// 对Date的扩展,将 Date 转化为指定格式的String;
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
//时间戳转时分秒
//(new Date(1615497700000)).Format("yyyy-MM-dd hh:mm:ss.S")===>2021-03-12 05:21:40.0
//获取年(new Date()).getFullYear() ===>2022
//获取时间戳的方法 new Date().getTime()方法可以返回当前时间的时间戳,精确到毫秒
var timestamp = new Date().getTime(); //结果为 1625821709636
//"yyyy-MM-ddThh:mm:ss"格式的时间给input赋值才有效,否则赋值不成功
var fromdate= (new Date()).Format("yyyy-MM-ddThh:mm:ss")
Date.prototype.Format = function (fmt) { // author: meizz
var o = {
"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+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
input标签 添加了step="1"时才会有秒,前提是已经添加了 type=“datetime-local”
2023-08-04T00:00:00 表示2023-08-04 00:00:00
<input type="datetime-local" step="1" value="2023-08-04T00:00:00"/>
var date=new Date("2023-10-14 17:41:04")
var disabtime=date.getTime();
1. var time1 = date.getTime(); 会精确到毫秒,前面new Date()时设置的时间
new Date().getTime(); 当前时间
2.var time2 = date.valueOf(); 会精确到毫秒
3.var time3 = Date.parse(date);只能精确到秒,毫秒用000替代
第一、第二种:会精确到毫秒,第三种:只能精确到秒,毫秒用000替代,注意:获取到的时间戳除以1000就可获得Unix时间戳,就可传值给后台得到。