2017-02-09 63 views
1

我得到當前的日期如下當前時間:5分鐘內加入到JavaScript的

var now = new Date(); 

我想5分鐘內加入到現有的時間。時間是12小時制。如果時間是上午3:46,那麼我想得到上午3: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); 

如何將5分鐘添加到「now」變量? 感謝

+8

[如何將30分鐘添加到JavaScript Date對象?](http://stackoverflow.com/questions/1197928/how-to-add-30-minutes-to-a-javascript-date-對象) – Philipp

+1

uhm ...'now.setMinutes(now.getMinutes()+ 5)'?不要懶惰。 – KarelG

+2

改爲使用MomentJS。儘管看起來很簡單,但時間太容易出錯。 –

回答

0

試試這個:

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

您應該使用getTime()方法。

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

+0

Hi @ Alexandru-lonut Mihai。是否有可能只返回上述示例中的12小時格式的時間 – venkat14

+0

您收到哪個錯誤? –

+0

TypeError:對象不支持屬性或方法'getTime'。當行命中= DateFormat(現在)時,我得到的值爲「9/2/2017/4:44 AM」。這個值傳遞給var next = addMinutes(now,5);在我之前的帖子中,我提到了12小時格式錯誤,我正在考慮獲取24小時制格式,我正在嘗試在ConvertTime方法中執行 – venkat14

1

得到分鐘,加入5並設置分鐘

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

 
console.log(s)

2

很容易與JS,但各種微微有點增加答案,這裏有一個方法來做到這一點與moment.js,這是一個流行的圖書館荷蘭國際集團的日期/時間:

https://jsfiddle.net/ovqqsdh1/

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

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

0

我給如何添加任何字符串形式的很短答案ny:nw:nd:nh:nm:ns其中n是一個數字到Date對象:

/** 
* 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); 
} 

測試:

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 

玩得開心!