preface
While there are mature and useful libraries for handling dates, such as Momentjs and date-Fns, we may not need the entire library in practice.
So I’ve compiled a complete list of dates and times in front end development. Some of it comes from ourselves, and some of it comes from our all-powerful Internet friends
Gets the current timestamp
var timestamp = Date.parse(new Date()); Var timestamp = (new Date()).valueof (); Var timestamp = new Date().gettime (); Var timestamp = +new Date(); var timestamp = Date.now();Copy the code
Gets the specified timestamp
var timestamp = (new Date("2019/10/24 08:00:00")).getTime();
var timestamp = (new Date("The 2019-10-24 08:00:00")).getTime();Copy the code
Gets the timestamp of the day before/after the current time
var timestamp = +new Date() - 24*60*60*1000;
var timestamp = +new Date() + 24*60*60*1000;Copy the code
Today’s zero hour time stamp
var timestamp = new Date(new Date().toLocaleDateString()).getTime();Copy the code
Today’s latest time: 23:59:59
let timestamp = new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000-1;Copy the code
Gets the timestamp n days after the current time
/** * @param {number} n Number of days * @returns {number} The returned value is time millisecond */function toNextTimes(n){
let timestamp = +new Date() + n * 86400000;
return timestamp;
}Copy the code
First day of the week
/ * * * * @return{*} WeekFirstDay returns the first day of the week */function showWeekFirstDay() {let Nowdate=new Date();
let WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
return WeekFirstDay;
}Copy the code
Last day of the week
/ * * * * @return{*} WeekLastDay Returns the last day of the week */function showWeekLastDay() {let Nowdate=new Date();
let WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
let WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000);
return WeekLastDay;
}Copy the code
First day of the month
/ * * * * @return{*} MonthFirstDay Returns the time of the first day of this month */function showMonthFirstDay() {let Nowdate=new Date();
let MonthFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth());
return MonthFirstDay;
}Copy the code
Last day of the month
/ * * * * @return{*} MonthLastDay Returns the time of the last day of the month */function showMonthLastDay() {let Nowdate=new Date();
let MonthNextFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth()+1);
let MonthLastDay=new Date(MonthNextFirstDay-86400000);
return MonthLastDay;
}Copy the code
Date to timestamp
/** * @param {String} time - A date String, such as'2018-8-8'.'2018,8,8'.'2018/8/8'* @returns {Number} Returns the time millisecond value */function timeToTimestamp (time) {
let date = new Date(time);
let timestamp = date.getTime();
return timestamp;
}
Copy the code
Formatting the current time
/ * * * * @return{string} timeText Returns the system time character string */function getdataTimeSec() {
let time = new Date();
let weekDay;
let year = time.getFullYear();
let month = time.getMonth() + 1;
letday = time.getDate(); // Get the minutes and secondslet h = time.getHours();
let m = time.getMinutes();
lets = time.getSeconds(); // Check if less than 10 h = check(h); m = check(m); s = check(s);let now_day = time.getDay();
switch (now_day) {
case 0: {
weekDay = "Sunday"
}
break;
case 1: {
weekDay = "Monday"
}
break;
case 2: {
weekDay = "Tuesday"
}
break;
case 3: {
weekDay = "Wednesday"
}
break;
case 4: {
weekDay = "Thursday"
}
break;
case 5: {
weekDay = "Friday"
}
break;
case 6: {
weekDay = "Saturday"
}
break;
case 7: {
weekDay = "Sunday"
}
break;
}
let timeText = year + "Year" + month + "Month" + day + "日 " + "" + weekDay + "" + h + ":" + m +":" + s;
return timeText
}
Copy the code
// If the time number is less than 10, a "0" is added before it.function check(i) {
let num;
i < 10 ? num = "0" + i : num = i;
return num;
}Copy the code
Returns the time interval between the specified timestamp
/** * @param {*} startTime startTime timestamp * @param {*} endTime endTime timestamp * @return{string} STR Returns the time string */function getTimeInterval(startTime, endTime) {
let runTime = parseInt((endTime - startTime) / 1000);
let year = Math.floor(runTime / 86400 / 365);
runTime = runTime % (86400 * 365);
let month = Math.floor(runTime / 86400 / 30);
runTime = runTime % (86400 * 30);
let day = Math.floor(runTime / 86400);
runTime = runTime % 86400;
let hour = Math.floor(runTime / 3600);
runTime = runTime % 3600;
let minute = Math.floor(runTime / 60);
runTime = runTime % 60;
let second = runTime;
let str = ' ';
if (year > 0) {
str = year + 'years';
}
if (year <= 0 && month > 0) {
str = month + 'month';
}
if (year <= 0 && month <= 0 && day > 0) {
str = day + 'day';
}
if (year <= 0 && month <= 0 && day <= 0 && hour > 0) {
str = hour + 'hour';
}
if (year <= 0 && month <= 0 && day <= 0 && hour <= 0 && minute > 0) {
str = minute + 'minutes';
}
if (year <= 0 && month <= 0 && day <= 0 && hour <= 0 && minute <= 0 && second > 0) {
str += second + '秒';
}
str += 'before';
return str;
}
Copy the code
Format the date by type
/** * @param {*} date Specifies the date variable * @param {string} dateType Specifies the return type * @return{string} dateText returns the date string */ in the specified formatfunction getFormatDate(date, dateType) {
let dateObj = new Date(date);
let month = dateObj.getMonth() + 1;
let strDate = dateObj.getDate();
let hours = dateObj.getHours();
let minutes = dateObj.getMinutes();
let seconds = dateObj.getSeconds();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
if (hours >= 0 && hours <= 9) {
hours = "0" + hours
}
if (minutes >= 0 && minutes <= 9) {
minutes = "0" + minutes
}
if (seconds >= 0 && seconds <= 9) {
seconds = "0" + seconds
}
let dateText = dateObj.getFullYear() + 'years' + (dateObj.getMonth() + 1) + 'month' + dateObj.getDate() + 'day';
if (dateType == "yyyy-mm-dd") {
dateText = dateObj.getFullYear() + The '-' + (dateObj.getMonth() + 1) + The '-' + dateObj.getDate();
}
if (dateType == "yyyy.mm.dd") {
dateText = dateObj.getFullYear() + '. ' + (dateObj.getMonth() + 1) + '. ' + dateObj.getDate();
}
if (dateType == "yyyy-mm-dd MM:mm:ss") {
dateText = dateObj.getFullYear() + The '-' + month + The '-' + strDate + ' ' + hours + ":" + minutes + ":" + seconds;
}
if (dateType == "mm-dd MM:mm:ss") {
dateText = month + The '-' + strDate + ' ' + hours + ":" + minutes + ":" + seconds;
}
if (dateType == "Yyyy mm: mm: SS") {
dateText = dateObj.getFullYear() + 'years' + month + 'month' + strDate + 'day' + ' ' + hours + ":" + minutes + ":" + seconds;
}
return dateText;
}Copy the code
Check whether it is a leap year
/** * @param {number} year Specifies the year * @return{Boolean} returns a Boolean */function leapYear(year) {
return! (year % (year % 100 ? 4:400)); }Copy the code
Returns the leap year between two years
/** * @param {number} start Start year * @param {number} end End year * @return{array} arr returns an array */ that complies with a leap yearfunction leapYears(start, end) {
let arr = [];
for (var i=start; i<end; i++) {
if ( leapYear(i) ) {
arr.push(i)
}
}
return arr
}Copy the code
Check whether the time format is valid
/** * short time, such as (10:24:06) * @param {string} STR short time to validate * @return{Boolean} returns a Boolean */functionIsTime (STR) {var a = str.match(/^(\d{1,2})(:)? (\ d {1, 2}) \ 2 $/ (\ d {1, 2}));if (a == null) { return false; }
if (a[1] >= 24 || a[3] >= 60 || a[4] >= 60) {
return false
}
return true; } /** * short date, such as (2019-10-24) * @param {string} STR short time to validate * @return{Boolean} returns a Boolean */functionStrDateTime (STR) {var result = STR. Match (/ ^ (\ d {1, 4}) (-) | \ / (\ d {1, 2}) \ 2 $/ (\ d {1, 2}));if (result == null) return false;
var d = new Date(result[1], result[3] - 1, result[4]);
return(d.getFullYear() == result[1] && d.getMonth() + 1 == result[3] && d.getDate() == result[4]); } @param {string} STR specifies the length of time required for validationreturn{Boolean} returns a Boolean */functionStrDateTime (STR) {var result = STR. Match (/ ^ (\ d {4}) (-) | \ / (\ d {1, 2}) \ 2 (\ d {1, 2}) (\ d {1, 2}) : (\ d {1, 2}) : (\ d {1, 2}) $/);if (result == null) return false;
var d = new Date(result[1], result[3] - 1, result[4], result[5], result[6], result[7]);
return (d.getFullYear() == result[1] && (d.getMonth() + 1) == result[3] && d.getDate() == result[4] && d.getHours() == result[5] && d.getMinutes() == result[6] && d.getSeconds() == result[7]);
}Copy the code
Validate date size
/ * * * :"2019-10-24" 和 "2019-10-25"* @param {string} d1 Date to be verified 1 * @param {string} d2 Date to be verified 2 * @return{Boolean} returns a Boolean */function compareDate(d1, d2) {
return ((new Date(d1.replace(/-/g, "/ /"))) < (new Date(d2.replace(/-/g, "/ /"))));
}Copy the code
Verify that a date is today
/** * @param {string} val The date to validate * @return{Boolean} returns a Boolean */function isToday(val){
return new Date().toLocaleDateString() == new Date(val).toLocaleDateString();
}Copy the code
Verify that the date passed in was yesterday
/** * @param {string} val The date to validate * @return{Boolean} returns a Boolean */function isYesterday(val) {
var today = new Date();
var yesterday = new Date(now - 1000 * 60 * 60 * 24);
var test = new Date(val);
if (yesterday.getYear() === test.getYear() && yesterday.getMonth() === test.getMonth() && yesterday.getDate() === test.getDate()) {
return true;
} else {
return false; }}Copy the code
Set the date a few days later
/** * @param {string} date Start date * @param {number} day Number of days back * @return{string} returns the desired date */function convertDate (date, day) {
let tempDate = new Date(date);
tempDate.setDate(tempDate.getDate()+day);
let Y = tempDate.getFullYear();
let M = tempDate.getMonth()+1 < 10 ? '0'+(tempDate.getMonth()+1) : tempDate.getMonth()+1;
let D = tempDate.getDate() < 10 ? '0'+(tempDate.getDate()) : tempDate.getDate();
let result = Y + "-" + M + "-" + D
return result;
}Copy the code
Write in the last
If there is an error in the above function, or there is some work encountered, but the above is not written, welcome to point out ~