Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add 5 minutes to current time javascript

I am getting the current date as below:

var now = new Date();

I want to add 5 minutes to the existing time. The time is in 12 hour format. If the time is 3:46 AM, then I want to get 3:51 AM.

function DateFormat(date) {
        var days = date.getDate();
        var year = date.getFullYear();
        var month = (date.getMonth() + 1);
        var hours = date.getHours();
        var minutes = date.getMinutes();
        var ampm = hours >= 12 ? 'PM' : 'AM';
        hours = hours % 12;
        hours = hours ? hours : 12; // the hour '0' should be '12'
        minutes = minutes < 10 ? '0' + minutes : minutes;
        var strTime = days + '/' + month + '/' + year + '/ ' + hours + ':' + minutes + ' ' + ampm;
     //   var strTime = hours + ':' + minutes + ' ' + ampm;
        return strTime;
    }

    function OnlyTime(date) {

            var days = date.getDate();
            var year = date.getFullYear();
            var month = (date.getMonth() + 1);
            var hours = date.getHours();
            var minutes = date.getMinutes();
            var ampm = hours >= 12 ? 'PM' : 'AM';
            hours = hours % 12;
            hours = hours ? hours : 12; // the hour '0' should be '12'
            minutes = minutes < 10 ? '0' + minutes : minutes;
           // var strTime = days + '/' + month + '/' + year + '/ ' + hours + ':' + minutes + ' ' + ampm;
              var strTime = hours + ':' + minutes + ' ' + ampm;
            return strTime;

    }

    function convertTime(time)
    {

        var hours = Number(time.match(/^(\d+)/)[1]);
        var minutes = Number(time.match(/:(\d+)/)[1]);
        var AMPM = time.match(/\s(.*)$/)[1];
        if (AMPM == "PM" && hours < 12) hours = hours + 12;
        if (AMPM == "AM" && hours == 12) hours = hours - 12;
        var sHours = hours.toString();
        var sMinutes = minutes.toString();
        if (hours < 10) sHours = "0" + sHours;
        if (minutes < 10) sMinutes = "0" + sMinutes;
        alert(sHours + ":" + sMinutes);
    }

    function addMinutes(date, minutes) {
        return new Date(date.getTime() + minutes * 60000);
    }

function convertTime(time)
    {

        var hours = Number(time.match(/^(\d+)/)[1]);
        var minutes = Number(time.match(/:(\d+)/)[1]);
        var AMPM = time.match(/\s(.*)$/)[1];
        if (AMPM == "PM" && hours < 12) hours = hours + 12;
        if (AMPM == "AM" && hours == 12) hours = hours - 12;
        var sHours = hours.toString();
        var sMinutes = minutes.toString();
        if (hours < 10) sHours = "0" + sHours;
        if (minutes < 10) sMinutes = "0" + sMinutes;
        alert(sHours + ":" + sMinutes);
    }

// calling way
  var now = new Date();
                now = DateFormat(now);
                var next = addMinutes(now, 5);

                next = OnlyTime(next);

                var nowtime = convertTime(next);

How to add 5 minutes to the "now" variable? Thanks

like image 603
venkat14 Avatar asked Feb 09 '17 08:02

venkat14


Video Answer


2 Answers

You should use getTime() method.

function AddMinutesToDate(date, minutes) {
    return new Date(date.getTime() + minutes * 60000);
}

function AddMinutesToDate(date, minutes) {
     return new Date(date.getTime() + minutes*60000);
}
function DateFormat(date){
  var days = date.getDate();
  var year = date.getFullYear();
  var month = (date.getMonth()+1);
  var hours = date.getHours();
  var minutes = date.getMinutes();
  minutes = minutes < 10 ? '0' + minutes : minutes;
  var strTime = days + '/' + month + '/' + year + '/ '+hours + ':' + minutes;
  return strTime;
}
var now = new Date();
console.log(DateFormat(now));
var next = AddMinutesToDate(now,5);
console.log(DateFormat(next));
like image 103
Mihai Alexandru-Ionut Avatar answered Oct 07 '22 05:10

Mihai Alexandru-Ionut


get minutes and add 5 to it and set minutes

var s = new Date();
console.log(s)
s.setMinutes(s.getMinutes()+5);

console.log(s)
like image 45
Vinod Louis Avatar answered Oct 07 '22 07:10

Vinod Louis