-3

i have mm-dd-YYYY H:i Date format, and i want to add 30 minutes in selected Date Time.

function SetEndDate(start_date){ // start_date = "05-25-2017 05:00"
        var duration     = '30';
        var end_date_tmp = new Date(start_date);

        end_date_tmp.setMinutes(duration);
        $('#end_date').val(end_date_tmp);
}

it gives Invalid Date Error.

Apurv Chaudhary
  • 1,672
  • 3
  • 30
  • 55

3 Answers3

2

Because your library uses a custom format ("05-25-2017 05:00"), I recommend trying out the fecha library. It handles the parsing/formatting properly cross browser.

function GetEndDate(start_date){ // start_date = "05-25-2017 05:00"
    var duration = 30;
    var d = fecha.parse(start_date, 'MM-DD-YYYY HH:mm');

    d.setMinutes(d.getMinutes() + duration);
    return fecha.format(d, 'MM-DD-YYYY HH:mm');;
}

console.log(GetEndDate('05-25-2017 05:00'));
//Call it like so:
//$('#end_date').val(GetEndDate(/* Your variable here */));
<script src="https://cdnjs.cloudflare.com/ajax/libs/fecha/2.3.1/fecha.min.js"></script>
Blue
  • 22,608
  • 7
  • 62
  • 92
  • I'm not saying that fetcha is bad, but try to avoid using other library when answering these type of question, because it's not necessary – Arthur Guiot May 22 '17 at 12:28
  • How is it not necessary @ArthurGuiot? Technically every question on stackoverflow doesn't require a library. This answer would become a lot more complicated without a library involved, and therefore I feel like it's a valid answer. – Blue May 22 '17 at 12:30
  • not hard to parse the string to pass to date object...or suggest OP change it's source which is why you are suggesting a library – charlietfl May 22 '17 at 12:39
1

Try like this:

var newDateObj = new Date();
newDateObj.setTime(oldDateObj.getTime() + (30 * 60 * 1000));

Like this:

function SetEndDate(start_date)
{ // start_date = "05-25-2017 05:00"
        var end_date_tmp = new Date(start_date);
         end_date_tmp.setTime(start_date.getTime() + (30 * 60 * 1000));
}

Refer this link

Community
  • 1
  • 1
User6667769
  • 745
  • 1
  • 7
  • 25
-1
function SetEndDate(start_date){ // start_date = "05-25-2017 05:00" (mm-dd-yyyy) format
var duration        = '30';

var my_date_format  = function(start_date){
    var d           = new Date(Date.parse(start_date.replace(/-/g, "/")));
    var month       = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
    var date        = d.getFullYear() + "-" + month[d.getMonth()] + "-" + d.getDate();
    var time        = d.getHours()+":"+d.getMinutes();
    var date_time   = date + " " + time;
    return (date_time);  
};

var date_time   = my_date_format(start_date);
    d2              = new Date ( date_time );
    d2.setTime(d2.getTime() + (duration * 60 * 1000));

var date_new    = new Date(d2);
    yr              = date_new.getFullYear();
    month           = date_new.getMonth() < 10 ? '0' + (date_new.getMonth() + 1)  : (date_new.getMonth() + 1);
    day             = date_new.getDate()  < 10 ? '0' + date_new.getDate()  : date_new.getDate();
    minutes         = date_new.getMinutes()  < 10 ? '0' + date_new.getMinutes()  : date_new.getMinutes();
    hours           = date_new.getHours()  < 10 ? '0' + date_new.getHours()  : date_new.getHours();
    time            = hours + ":" + minutes;
    newDate         = month + '-' + day + '-' + yr + ' ' + time;
$('#end_date').val(newDate); // you can get new date in "newDate" variable
}
Apurv Chaudhary
  • 1,672
  • 3
  • 30
  • 55
  • 1
    generally, code-only answers are frowned upon (more so for uncommented code): can you summarise & comment the idea coded? – greybeard Jul 10 '17 at 07:42