0

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.

4
  • What do you mean by 'javascript date'? Is it a string or Date? What should output be? String? Commented May 4, 2015 at 8:57
  • 1
    did you try to programm something what we can review? Commented May 4, 2015 at 8:58
  • see the added sample fiddle @Axel Commented May 4, 2015 at 9:19
  • Have you had a look at this library: momentjs.com It comes with tons of formatting and conversion methods. Commented May 4, 2015 at 9:56

3 Answers 3

1

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. :)

Sign up to request clarification or add additional context in comments.

Comments

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

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.