0

I have a date string, want to convert it into a date object, add 2 hours and print out the converted date object back to a variable. But I get the error listed bellow:

// dateTime: 2013-09-27 09:50:05 
var dateTime = $("#inputDatetime").val();
var startDate = dateTime;
var date = new Date(startDate);
var duration = 2;
var endDate = date;
endDate.setHours(date.getHours()+duration)
var dateString = endDate.format("dd-m-yy hh:mm:ss");

Error:

Uncaught TypeError: Object [object Date] has no method 'format'

Why do I get this TypeError?

1
  • Are you getting the value from datepicker as string? Commented Sep 6, 2013 at 18:10

4 Answers 4

3

Vaibs, there is no method "format", you can do formating using available methods from Date Object. please don't use plugin

example :

// dd-mm-yy hh:mm:ss

function formatDate(date) {
    return ((date.getDate()<10?'0':'')+date.getDate()) + "-"+ 
        (((date.getMonth()+1)<10?'0':'') + (date.getMonth()+1)) + "-" + 
        date.getFullYear()  + " " +((date.getHours()<10?'0':'')+date.getHours()) + ":" + 
       (date.getMinutes()<10?'0':'') +  date.getMinutes() + ":" + 
       (date.getSeconds()<10?'0':'')  + date.getSeconds(); 
 }

*thank you @donot

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

3 Comments

Mah, sorry. I just noticed that I deleted two characters too much, so the third line is a string now. My bad... x: You might wanna fix that, too.
start: 2013-09-03 14:00:00, end: 3-9-2013 15:0:0
@doonot any mistake from my side .? actually final format required is "dd-m-yy hh:mm:ss"
1

Use jquery ui date parser.

http://docs.jquery.com/UI/Datepicker/parseDate

This is the best function for parsing dates out of strings that I've had the pleasure to work with in js. And as you added the tag jquery it's probably the best solution for you.

1 Comment

An additional library just for formatting a date? Sounds like overdoing it if you ask me...
0

.format() is not a valid Date method. JavaScript does not have an easy way to format dates and times with a user-specified format. The best you can do (without separate plugins) is to create your own string by getting each component of the date separately, and formatting/concatenating them.

Comments

0

I've used this before and it seems to work!

var dateString = endDate.toString("dd-m-yy hh:mm:ss");

3 Comments

Wow thx, that worked, but it is printing this: Sat Sep 28 2013 14:10:57 GMT+0200 (CEST). Any idea how to get rid of GMT..?
You didn't even test your code, did you? It's nothing close to the format you put there... at least not on Chrome. So at the very least it's not cross-plattform.
I feel like I'll be judged horribly for suggesting this, cos theres probably a much fancier way, but you could try using the substring() method. for example dateString.substring(0, 24); I believe that will chop off the GTM bit at the end, so to speak! But I'm not pro. Good luck!

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.