Some wonderful date conversion in javascript

Keywords: Front-end

1. Get today's 0:0:0:0 seconds (usually used to get the start date)

new Date(new Date().toLocaleDateString());  // Mon Nov 12 2018 00:00:00 GMT+0800 (China standard time)

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 (China standard time)

3. Get 0:00:00 of a month ago

new Date(new Date(new Date().toLocaleDateString()).setMonth(new Date().getMonth()-1));
//Fri Oct 12 2018 00:00:00 GMT+0800 (China standard time)

4. Get the date of the previous day

new Date(new Date().setDate(new Date().getDate()-1)); // Sun Nov 11 2018 17:21:56 GMT+0800 (China standard time)

5. plus 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 (China standard time)

6. minus 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 (China standard time)

7. Get today's 23:59:59

new Date(new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000-1); // Mon Nov 12 2018 23:59:59 GMT+0800 (China standard time)

7. 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 (China standard time)

8. Get 23:59:59 tomorrow

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 (China standard time)


8. Get the current time tomorrow at 23:59:59


var curTime = 'Wed Nov 14 2018 17:38:31 GMT+0800 (China standard time)';
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 (China standard time)

9. Obtain 23:59:59 of yesterday's current time


var curTime = 'Wed Nov 14 2018 17:38:31 GMT+0800 (China standard time)';
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 (China standard time)


10. Hours, minutes and seconds before the end of the activity (countdown)


var endTime = function (startTime) {
    setInterval(
        function(){
            var str = '';
            var date1 = new Date(startTime);  //start time
            var date2 = new Date();     //End time
            var date3 = date1.getTime() - date2.getTime();   //Milliseconds of time difference
            if (date3>0){
                // //Number of years to calculate the difference
                // var years = Math.floor(date3 / (12 * 30 * 24 * 3600 * 1000));
                // //Number of months to calculate the difference
                var leave = date3 % (12 * 30 * 24 * 3600 * 1000);
                // var months = Math.floor(leave / (30 * 24 * 3600 * 1000));
                // //Calculate the difference days
                var leave0 = leave % (30 * 24 * 3600 * 1000);
                var days = Math.floor(date3 / (24 * 3600 * 1000));
                //Calculate the hours
                var leave1 = leave0 % (24 * 3600 * 1000);     //Milliseconds remaining after days
                var hours = Math.floor(leave1 / (3600 * 1000));
                //Calculate difference minutes
                var leave2 = leave1 % (3600 * 1000);         //Milliseconds left after hours
                var minutes = Math.floor(leave2 / (60 * 1000));
                //Calculate the number of seconds difference
                var leave3 = leave2 % (60 * 1000);       //Number of milliseconds left after counting minutes
                var seconds = Math.round(leave3 / 1000);
                if (days > 0){
                    str += '<span>'+ days +'</span>day';
                }
                if (hours > 0){
                    str += '<span>'+ hours +'</span>hour';
                }
                str += '<span>'+ minutes +'</span>Minute<span>'+ seconds +'</span>second';
            }else {
                str = 'Expired'
            }
            console.log(str)
        },1000
    );
};


A stupid farmer, my world can only learn for life!

Posted by phisher1 on Sat, 07 Dec 2019 22:09:00 -0800