It's hard enough to fake as much false data as possible.The last note recorded how to get a random license plate. This note recorded how to get a random time.That's not so much nonsense, just code it
// Gets a random number in a specified range var getRangeRandomNumber = function(num1,num2){ num1 = Number.isInteger(num1) ? num1: 0; num2 = Number.isInteger(num2) ? num2: 0; var minNum=Math.min(num1,num2),maxNum=Math.max(num1,num2); return Math.round(Math.random() * (maxNum - minNum)) + minNum; }; // Date extension: formatted date var dateExtendFormat = function(date, format) { var o = { "M+": date.getMonth() + 1, "d+": date.getDate(), "H+": date.getHours(), "m+": date.getMinutes(), "s+": date.getSeconds(), "q+": Math.floor((date.getMonth() + 3) / 3), "S": date.getMilliseconds() } if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) { if (new RegExp("(" + k + ")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); } } return format; }; // Get Random Time for a Specified Range, Dependent on Method getRangeRandomNumber,dateExtendFormat var getRangeRandomDate=function(date1,date2,format){ var date1ValueOf=new Date(date1).valueOf(),date2ValueOf=new Date(date2).valueOf(); if(isNaN(date1ValueOf)&&isNaN(date2ValueOf)){ date1ValueOf=0; date2ValueOf=new Date().valueOf(); } else{ if(isNaN(date1ValueOf))date1ValueOf=0; if(isNaN(date2ValueOf))date2ValueOf=0; } var retDate=new Date(getRangeRandomNumber(Math.abs(date1ValueOf-date2ValueOf)) + Math.min(date1ValueOf,date2ValueOf)); if(format){ retDate=dateExtendFormat(retDate,format); } return retDate; };
- Stick in the test code
console.log("*********************************************************Test 1: Yes Date type*********************************************************"); for(var i=0;i<5;i++){ console.log(getRangeRandomDate()); } console.log("*********************************************************Test 2: Returns the string time in the specified format*********************************************************"); for(var i=0;i<5;i++){ console.log(getRangeRandomDate(undefined,"2019-11-07","yyyy-MM-dd HH:mm:ss")); } console.log("*********************************************************Test 3: Returns the string time in the specified format*********************************************************"); for(var i=0;i<5;i++){ console.log(getRangeRandomDate("2019-11-07","I am not the time","yyyy-MM-dd HH:mm:ss.S")); } console.log("*********************************************************Test 4: Returns the string time in the specified format*********************************************************"); for(var i=0;i<5;i++){ console.log(getRangeRandomDate("2019-11-07","2019-11-06 23:59:59","HH:mm:ss")); }
Take a look at the test results
If it weren't for some nonsense, it would be short enough...But there's no doubt about this, so let's finish!
Reference links: javascript Date format(js date format)