js处理时间格式new Date()

it2026-02-27  5

js处理时间格式new Date()

new Date()方法存在兼容性问题,项目开发中遇到时间格式显示为NaN-NaN-NaN:

const strTime="2020-10-1 09:10:10"; const myDate= new Date(strTime); const Y = myDate.getFullYear(); const M = myDate.getMonth()+1; const D = myDate.getDate(); const curDay = Y + '-'+ M + '-' + D; console.log(curDay);

输出结果:Android系统:2018-1-1 iOS 系统:NaN-NaN-NaN ios ‘2015-05-04’是无法被IOS系统,使用new Date(str)来正确生成日期对象的。 正确的用法是’2015/05/05’. 在Chrome下是可以正确执行的,但是在其他浏览器下也会出现这种问题。 以下是一些时间格式的转换与时间的获取:

var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-????) myDate.getMonth(); //获取当前月份(0-11,0代表1月) // 所以获取当前月份是myDate.getMonth()+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( ); //获取日期与时间

JS获取当前时间戳的方法-JavaScript 获取当前毫秒时间戳有以下三种方法:

var timestamp =Date.parse(new Date()); 结果:1280977330000 //不推荐; 毫秒改成了000显示 var timestamp =(new Date()).valueOf(); 结果:1280977330748 //推荐; var timestamp=new Date().getTime(); 结果:1280977330748 //推荐;

注:js中单独调用new Date(); 显示这种格式 Mar 31 10:10:43 UTC+0800 2012;但是用new Date() 参与计算会自动转换为从1970.1.1开始的毫秒数

时间戳转为日期格式方法:
//时间戳转日期格式 var formatDateTime3 = function(time, format){ var t = new Date(time); var tf = function(i){return (i < 10 ? '0' : '') + i}; return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){ switch(a){ case 'yyyy': return tf(t.getFullYear()); break; case 'MM': return tf(t.getMonth() + 1); break; case 'mm': return tf(t.getMinutes()); break; case 'dd': return tf(t.getDate()); break; case 'HH': return tf(t.getHours()); break; case 'ss': return tf(t.getSeconds()); break; } }) };

可以给Date添加扩展函数format

/** *对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 */ Date.prototype.format = function (fmt) { 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; }

如何使用

var day = new Date(); var time = day1.format("yyyy-MM-dd");
时间格式字符串转为时间戳(毫秒)
var time1=‘2016-01-01 17:22:37’; var date=new Date(time1.replace(/-/g, '/')); //开始时间 var time2=date.getTime();
如何将2015-03-12 12:00 转换成标准时间
var parserDate = function (date) { var t = Date.parse(date); //获取时间戳 if (!isNaN(t)) { return new Date(Date.parse(date.replace(/-/g, "/"))); } else { return new Date(); } };

js方法返回值:Thu Mar 19 2015 12:00:00 GMT+0800 (中国标准时间)

判断时间间隔不超过24个小时
var date1=new Date("2004/09/16 20:08:00"); var date2=new Date("2004/09/17 20:08:00"); var date3=(date2.getTime()-date1.getTime())/1000; //相差秒数 if(date3>60*60*24*1000){ alert("开始时间与结束时间间隔大于24小时!"); return false; }

这几个方法应该够用,以后再继续扩展.

最新回复(0)