0

I have a string 10/11/2012 meaning November 10, 2012.

But when I do new Date("10/11/2012") it returns October 11th.

How do I pass in the date format I want? In this case dd-mm-yyyy

2
  • You can't. You could extend the native javascript date parser so it becomes possible though. Commented Nov 9, 2012 at 20:00
  • A little bit of a tangent, but the topic is how to handle locale when dealing with Date objects stackoverflow.com/questions/6356839/… Remember, date objects represent a date in time, but that time is represented as a different hour of day depending on where you are Commented Nov 9, 2012 at 20:09

3 Answers 3

2

I found jQuery.datepicker.parseDate(format, Date) at this site:

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

So I will be using the jQuery datepicker instead.

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

Comments

1

Unfortunately, there's no JavaScript Date constructor that allows you to pass in culture information so that it uses localized date formats. Your best bet is to use the constructor that takes the year, month, and day separately:

var parts = dateString.split('/');
var date = new Date(parseInt(parts[2], 10), 
                    parseInt(parts[1], 10), 
                    parseInt(parts[0], 10));

4 Comments

Wouldn't a parseInt be needed? Does the Date constructor accept strings?
Yes, it would need parseInt. Added.
@jdwire it can deal with both
there is a implicit conversion : '5'==5 //true
0

For this specific case, you can use:

var dateparts = date.split("/");
var datestring = dateparts[1] + "/" + dateparts[0] + "/" +  dateparts[2];
var date = new Date(datestring);

In the more general case, you can extend the Date prototype, as demonstrated in this answer:

https://stackoverflow.com/a/13163314/1726343

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.