How do I subtract minutes from a date in javascript?

后端 未结 9 2140
囚心锁ツ
囚心锁ツ 2020-11-29 03:25

How can I translate this pseudo code into working js [don\'t worry about where the end date comes from except that it\'s a valid javascript date].

var myEndD         


        
相关标签:
9条回答
  • 2020-11-29 03:53
    var date=new Date();
    
    //here I am using "-30" to subtract 30 minutes from the current time.
    var minute=date.setMinutes(date.getMinutes()-30); 
    
    console.log(minute) //it will print the time and date according to the above condition in Unix-timestamp format.
    

    you can convert Unix timestamp into conventional time by using new Date().for example

    var extract=new Date(minute)
    console.log(minute)//this will print the time in the readable format.
    
    0 讨论(0)
  • 2020-11-29 03:56

    Everything is just ticks, no need to memorize methods...

    var aMinuteAgo = new Date( Date.now() - 1000 * 60 );
    

    or

    var aMinuteLess = new Date( someDate.getTime() - 1000 * 60 );
    

    update

    After working with momentjs, I have to say this is an amazing library you should check out. It is true that ticks work in many cases making your code very tiny and you should try to make your code as small as possible for what you need to do. But for anything complicated, use momentjs.

    0 讨论(0)
  • 2020-11-29 03:56

    Try as below:

    var dt = new Date();
    dt.setMinutes( dt.getMinutes() - 20 );
    console.log('#####',dt);
    
    0 讨论(0)
提交回复
热议问题