Home >Web Front-end >JS Tutorial >Some weird date conversions in javascript
This article brings you some weird date conversions in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
【Recommended reading: How to convert date format in JS】
1. Get today’s 0 hour 0 minutes and 0 seconds (commonly used to obtain the start date)
new Date(new Date()。toLocaleDateString()); // Mon Nov 12 2018 00:00:00 GMT+0800 (中国标准时间)
2. Get the date one month ago
new Date(new Date()。setMonth(new Date()。getMonth()-1)); //Fri Oct 12 2018 17:20:07 GMT+0800 (中国标准时间)
3. Get the 0 hours, 0 minutes and 0 seconds one month ago
new Date(new Date(new Date()。toLocaleDateString())。setMonth(new Date()。getMonth()-1)); //Fri Oct 12 2018 00:00:00 GMT+0800 (中国标准时间)
4. Get the previous day’s date
new Date(new Date()。setDate(new Date()。getDate()-1)); // Sun Nov 11 2018 17:21:56 GMT+0800 (中国标准时间)
5. Add one day
var dateTime=new Date(); dateTime=dateTime.setDate(dateTime.getDate()+1); dateTime=new Date(dateTime); // Tue Nov 13 2018 17:24:32 GMT+0800 (中国标准时间)
6. Subtract one day
var dateTime=new Date(); dateTime=dateTime.setDate(dateTime.getDate()-1); dateTime=new Date(dateTime); // Sun Nov 11 2018 17:25:12 GMT+0800 (中国标准时间)
7. Get today’s 23:59:59 Seconds
new Date(new Date(new Date()。toLocaleDateString())。getTime()+24*60*60*1000-1); // Mon Nov 12 2018 23:59:59 GMT+0800 (中国标准时间)
Get yesterday’s 23:59:59
var dateTime = new Date() dateTime = dateTime.setDate(dateTime.getDate() -1 ) dateTime = new Date(new Date(new Date(dateTime)。toLocaleDateString())。getTime() + 24 * 60 * 60 * 1000 - 1) // Sun Nov 11 2018 23:59:59 GMT+0800 (中国标准时间)
8. Get tomorrow’s 23:59:59
var dateTime = new Date() dateTime = dateTime.setDate(dateTime.getDate() + 1 ) dateTime = new Date(new Date(new Date(dateTime)。toLocaleDateString())。getTime() + 24 * 60 * 60 * 1000 - 1) // Tue Nov 13 2018 23:59:59 GMT+0800 (中国标准时间)
Get tomorrow’s 23:00 of the current time 59 minutes and 59 seconds
var curTime = 'Wed Nov 14 2018 17:38:31 GMT+0800 (中国标准时间)'; var dateTime = new Date(curTime) dateTime = dateTime.setDate(dateTime.getDate() + 1 ) dateTime = new Date(new Date(new Date(dateTime)。toLocaleDateString())。getTime() + 24 * 60 * 60 * 1000 - 1) // Thu Nov 15 2018 23:59:59 GMT+0800 (中国标准时间)
9. Get yesterday’s current time of 23 hours, 59 minutes and 59 seconds
var curTime = 'Wed Nov 14 2018 17:38:31 GMT+0800 (中国标准时间)'; var dateTime = new Date(curTime) dateTime = dateTime.setDate(dateTime.getDate() - 1 ) dateTime = new Date(new Date(new Date(dateTime)。toLocaleDateString())。getTime() + 24 * 60 * 60 * 1000 - 1) // Tue Nov 13 2018 23:59:59 GMT+0800 (中国标准时间)
10. How many hours, minutes and seconds are left before the event ends (countdown)
var endTime = function (startTime) { setInterval( function(){ var str = ''; var date1 = new Date(startTime); //开始时间 var date2 = new Date(); //结束时间 var date3 = date1.getTime() - date2.getTime(); //时间差的毫秒数 if (date3>0){ // //计算相差的年数 // var years = Math.floor(date3 / (12 * 30 * 24 * 3600 * 1000)); // //计算相差的月数 var leave = date3 % (12 * 30 * 24 * 3600 * 1000); // var months = Math.floor(leave / (30 * 24 * 3600 * 1000)); // //计算出相差天数 var leave0 = leave % (30 * 24 * 3600 * 1000); var days = Math.floor(date3 / (24 * 3600 * 1000)); //计算出小时数 var leave1 = leave0 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数 var hours = Math.floor(leave1 / (3600 * 1000)); //计算相差分钟数 var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数 var minutes = Math.floor(leave2 / (60 * 1000));
The above is a complete introduction to some weird date conversions in JavaScript. If you want to know more about JavaScript Tutorial, please pay attention to the PHP Chinese website.
The above is the detailed content of Some weird date conversions in javascript. For more information, please follow other related articles on the PHP Chinese website!