Is there any way to define a javascript date object with AM/PM value?
Something like this
var startDate = new Date("1900-1-1 8:20:00 PM");
Is there any way to define a javascript date object with AM/PM value?
Something like this
var startDate = new Date("1900-1-1 8:20:00 PM");
Share
Improve this question
edited Dec 24, 2011 at 12:40
Šime Vidas
186k65 gold badges286 silver badges391 bronze badges
asked Dec 24, 2011 at 12:21
Pawan NogariyaPawan Nogariya
8,97013 gold badges59 silver badges128 bronze badges
8
|
Show 3 more comments
4 Answers
Reset to default 12This works:
new Date( '1 Jan 1900 8:20:00 PM' )
and is equivalent to
new Date( '1 Jan 1900 20:20:00' )
Live demo: http://jsfiddle.net/cVE2E/
you can use Date.parse
var startDate = new Date(Date.parse("1900-1-1 8:20:00 PM"));
This depends on the browser and/or the locale. But I found a script that can help: http://blog.stevenlevithan.com/archives/date-time-format
There is no guarantee that the Date.parse() method, and hence the new Date() constructor can parse any specific date format. By the ECMAScript standard, only a specific ISO 8601 format and some implementation-dependent formats need to be processed.
Thus, for portability at least, you need to use other tools, such as the Globalize.js library; using it, you would use
Globalize.parseDate('1900-1-1 8:20:00 PM','yyyy-M-d h:mm:ss tt')
which returns a Date object when the first argument matches the format specified by the second argument.
If you need to process alternative date formats on input, you may need to write code that tries reading the data using specific formats until it gets a non-null result.
8PM
time should return 20, not 8. – Šime Vidas Commented Dec 24, 2011 at 12:37