日期格式与javascript中的时间范围

时间:2015-05-04 08:56:04

标签: javascript jquery date datetime format

我有一个javascript日期变量为04/05/2015, 01:30(dd / mm / yyyy,HH:mm)格式。现在我该如何将格式更改为04/05/2015, 01:00-01:30格式。即,我想改变时间范围的时间,其中第一时间值总是比第二时间值小30分钟。因此,如果日期为04/05/2015, 13:00,则格式化日期为04/05/2015, 12:30-13:30

编辑:有关示例,请参阅fiddle here

3 个答案:

答案 0 :(得分:1)

Im affraid that there is no out-of-the-box functionality for what you are asking, and you will have to write your own function for that.

Here is a js Date object specification : Date Object

您的新函数返回类型不能是Date,因为这种格式化只能通过字符串类型实现。

答案 1 :(得分:1)

您无法以该格式拥有date个对象。您将手动创建格式。它将是字符串。

var dateObj = new Date('04/05/2015, 01:30'), // input date 
    interval = 30,                           // interval in minutes
    remainingInterval = 0;

var hours = dateObj.getHours(),
    minutes = dateObj.getMinutes();
if(minutes > interval) {
    minutes = minutes - interval;
} else {
    remainingInterval  = interval - minutes;
    minutes = 60;
    hours = hours - 1;
    minutes = minutes - remainingInterval;
}

结果日期可以是

console.log(dateObj.getDate()+'/'+dateObj.getMonth()+'/'+dateObj.getFullYear()+', '+dateObj.getHours()+':'+dateObj.getMinutes()+' - '+hours+':'+minutes);

答案 2 :(得分:1)

Please check the below solutions:

http://jsfiddle.net/ub942s6y/14/

  

您需要更改data.addColumn(' datetime',' Date');到'字符串'因为我们正在改变时间

It will work fine. :)