Pertanyaan Tambahkan 5 menit ke javascript waktu saat ini


Saya mendapatkan tanggal saat ini seperti di bawah ini:

var now = new Date();

Saya ingin menambahkan 5 menit ke waktu yang ada. Waktu dalam format 12 jam. Jika waktunya 3:46, maka saya ingin mendapatkan 03:51.

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);

Bagaimana cara menambahkan 5 menit ke variabel "sekarang"? Terima kasih


4
2018-02-09 08:47


asal


Jawaban:


Kamu harus menggunakan getTime() metode.

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));


5
2018-02-09 08:50



Cukup mudah dengan JS, tetapi untuk menambahkan sedikit variasi pada jawaban, berikut ini cara melakukannya dengan moment.js, yang merupakan pustaka populer untuk menangani tanggal / waktu:

https://jsfiddle.net/ovqqsdh1/

var now = moment();
var future = now.add(5, 'minutes');
console.log(future.format("YYYY-MM-DD hh:mm"))

2
2018-02-09 08:55



dapatkan menit dan tambahkan 5 menit dan setel menit

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

console.log(s)


1
2018-02-09 08:51



//Date objects really covers milliseconds since 1970, with a lot of methods
//The most direct way to add 5 minutes to a Date object on creation is to add (minutes_you_want * 60 seconds * 1000 milliseconds)
var now = new Date(Date.now() + (5 * 60 * 1000));
console.log(now, new Date());


1
2018-02-09 08:55



Coba ini:

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

0
2018-02-09 08:49



Saya akan berikan sangat singkat jawaban tentang cara menambahkan apa saja string formulir ny:nw:nd:nh:nm:ns dimana n adalah angka ke Date obyek:

/**
 * Adds any date string to a Date object.
 * The date string can be in any format like 'ny:nw:nd:nh:nm:ns' where 'n' are
 * numbers and 'y' is for 'year', etc. or, you can have 'Y' or 'Year' or 
 * 'YEar' etc.
 * The string's delimiter can be anything you like.
 * 
 * @param Date date The Date object
 * @param string t The date string to add
 * @param string delim The delimiter used inside the date string
 */
function addDate (date, t, delim) {
   var delim = (delim)? delim : ':',
       x = 0,
       z = 0,
       arr = t.split(delim);

   for(var i = 0; i < arr.length; i++) {
      z = parseInt(arr[i], 10);
      if (z != NaN) {
         var y = /^\d+?y/i.test(arr[i])? 31556926: 0; //years
         var w = /^\d+?w/i.test(arr[i])? 604800: 0;   //weeks
         var d = /^\d+?d/i.test(arr[i])? 86400: 0;    //days
         var h = /^\d+?h/i.test(arr[i])? 3600: 0;     //hours
         var m = /^\d+?m/i.test(arr[i])? 60: 0;       //minutes
         var s = /^\d+?s/i.test(arr[i])? 1: 0;        //seconds
         x += z * (y + w + d + h + m + s);
      }
   }
   date.setSeconds(date.getSeconds() + x);
}

Menguji:

var x = new Date();
console.log(x);    //before
console.log('adds 1h:6m:20s');
addDate(x, '1h:6m:20s');
console.log(x);   //after
console.log('adds 13m/30s');
addDate(x, '13m/30s', '/');
console.log(x);   //after

Selamat bersenang-senang!


0
2017-12-02 12:41