How do I subtract minutes from a date in javascript?

后端 未结 9 2137
囚心锁ツ
囚心锁ツ 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:34

    Extend Date class with this function

    // Add (or substract if value is negative) the value, expresed in timeUnit
    // to the date and return the new date.
    Date.dateAdd = function(currentDate, value, timeUnit) {
    
        timeUnit = timeUnit.toLowerCase();
        var multiplyBy = { w:604800000,
                         d:86400000,
                         h:3600000,
                         m:60000,
                         s:1000 };
        var updatedDate = new Date(currentDate.getTime() + multiplyBy[timeUnit] * value);
    
        return updatedDate;
    };
    

    So you can add or substract a number of minutes, seconds, hours, days... to any date.

    add_10_minutes_to_current_date = Date.dateAdd( Date(), 10, "m");
    subs_1_hour_to_a_date = Date.dateAdd( date_value, -1, "h");
    
    0 讨论(0)
  • 2020-11-29 03:36

    This is what I did: see on Codepen

    var somedate = 1473888180593;
    var myStartDate;
    //var myStartDate = somedate - durationInMuntes;
    
    myStartDate = new Date(dateAfterSubtracted('minutes', 100));
    
    alert("The event will start on " + myStartDate.toDateString() + " at " + myStartDate.toTimeString());
    
    function dateAfterSubtracted(range, amount){
        var now = new Date();
        if(range === 'years'){
            return now.setDate(now.getYear() - amount);
        }
        if(range === 'months'){
            return now.setDate(now.getMonth() - amount);
        }
        if(range === 'days'){
            return now.setDate(now.getDate() - amount);
        }
        if(range === 'hours'){
            return now.setDate(now.getHours() - amount);
        }
        if(range === 'minutes'){
            return now.setDate(now.getMinutes() - amount);
        }
        else {
            return null;
        }
    }
    
    0 讨论(0)
  • 2020-11-29 03:40

    This is what I found:

    //First, start with a particular time
    var date = new Date();
    
    //Add two hours
    var dd = date.setHours(date.getHours() + 2);
    
    //Go back 3 days
    var dd = date.setDate(date.getDate() - 3);
    
    //One minute ago...
    var dd = date.setMinutes(date.getMinutes() - 1);
    
    //Display the date:
    var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    var date = new Date(dd);
    var day = date.getDate();
    var monthIndex = date.getMonth();
    var year = date.getFullYear();
    var displayDate = monthNames[monthIndex] + ' ' + day + ', ' + year;
    alert('Date is now: ' + displayDate);
    

    Sources:

    http://www.javascriptcookbook.com/article/Perform-date-manipulations-based-on-adding-or-subtracting-time/

    https://stackoverflow.com/a/12798270/1873386

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

    Once you know this:

    • You can create a Date by calling the constructor with milliseconds since Jan 1, 1970.
    • The valueOf() a Date is the number of milliseconds since Jan 1, 1970
    • There are 60,000 milliseconds in a minute :-]

    ...it isn't so hard.

    In the code below, a new Date is created by subtracting the appropriate number of milliseconds from myEndDateTime:

    var MS_PER_MINUTE = 60000;
    var myStartDate = new Date(myEndDateTime - durationInMinutes * MS_PER_MINUTE);
    
    0 讨论(0)
  • 2020-11-29 03:47

    moment.js has some really nice convenience methods to manipulate date objects

    The .subtract method, allows you to subtract a certain amount of time units from a date, by providing the amount and a timeunit string.

    var now = new Date();
    // Sun Jan 22 2017 17:12:18 GMT+0200 ...
    var olderDate = moment(now).subtract(3, 'minutes').toDate();
    // Sun Jan 22 2017 17:09:18 GMT+0200 ...
    
    0 讨论(0)
  • 2020-11-29 03:53

    You can also use get and set minutes to achieve it:

    var endDate = somedate;
    
    var startdate = new Date(endDate);
    
    var durationInMinutes = 20;
    
    startdate.setMinutes(endDate.getMinutes() - durationInMinutes);
    
    0 讨论(0)
提交回复
热议问题