mpsbhat
mpsbhat

Reputation: 2773

Date format with time range in javascript

I have a javascript date variable as 04/05/2015, 01:30 (dd/mm/yyyy, HH:mm) format. Now how can I change that format to 04/05/2015, 01:00-01:30 format. Ie, I want to change the time with time range where the first time value is always 30 minutes less than second time value. So If the date is 04/05/2015, 13:00 then the formatted date would be 04/05/2015, 12:30-13:30

EDIT: See the fiddle here for the sample.

Upvotes: 0

Views: 2752

Answers (3)

Aftab Muni
Aftab Muni

Reputation: 171

Please check the below solutions:

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

You need to change data.addColumn('datetime', 'Date'); to 'string' as we are changing time

It will work fine. :)

Upvotes: 1

sachin.ph
sachin.ph

Reputation: 1078

You can't have date object in that format. You will have manually create the format. It will be string.

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

resulting date can be

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

Upvotes: 1

Mladen Oršolić
Mladen Oršolić

Reputation: 1422

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

Your new function return type cannot be Date, as this kind of formatting can be only achieved with string type.

Upvotes: 1

Related Questions