I have some code that tries to parse a date string.
When I do alert(Date("2010-08-17 12:09:36"));
It properly parses the date and everything works fine but I can't call the methods associated with Date
, like getMonth()
.
When I try:
var temp = new Date("2010-08-17 12:09:36"); alert(temp);
I get an "invalid date" error.
Any ideas on how to parse "2010-08-17 12:09:36" with new Date()?
new Date creates a new Date object that you can modify or initialize with a different date while Date returns a string of the current date/time, ignoring its arguments.
Date() returns an implementation dependent string representing the current date and time. new Date() returns a Date object that represents the current date and time. ;-) Yeah, I know.
An object that represents a date and time. Internally the time is stored as the number of milliseconds since 01 January, 1970 UTC. Use new Date() to get a Date for the current time or Date. now() to get the current time in milliseconds since 01 January, 1970 UTC. Spec.
"The expression new Date() returns the current time in internal format, as an object containing the number of milliseconds elapsed since the start of 1970 in UTC.
new Date () creates a new date object with the current date and time: Date objects are static. The computer time is ticking, but date objects are not. new Date ( year, month, ...) new Date ( year, month, ...) creates a new date object with a specified date and time.
new Date (dateString) creates a new date object from a date string: Date strings are described in the next chapter. JavaScript stores dates as number of milliseconds since January 01, 1970, 00:00:00 UTC (Universal Time Coordinated). Zero time is January 01, 1970 00:00:00 UTC.
EDIT: new Date (dateString) uses one of these formats: Show activity on this post. The following format works in all browsers: So, to make a yyyy-mm-dd hh:mm:ss formatted date string fully browser compatible you would have to replace dashes with slashes:
Note that MDN strongly discourages the use of new Date (dateString) or Date.parse (dateString): Note: Parsing of date strings with the Date constructor (and Date.parse (), which works the same way) is strongly discouraged due to browser differences and inconsistencies.
Date()
With this you call a function called Date()
. It doesn't accept any arguments and returns a string representing the current date and time.
new Date()
With this you're creating a new instance of Date.
You can use only the following constructors:
new Date() // current date and time new Date(milliseconds) //milliseconds since 1970/01/01 new Date(dateString) new Date(year, month, day, hours, minutes, seconds, milliseconds)
So, use 2010-08-17 12:09:36
as parameter to constructor is not allowed.
See w3schools.
EDIT: new Date(dateString)
uses one of these formats:
The following format works in all browsers:
new Date("2010/08/17 12:09:36");
So, to make a yyyy-mm-dd hh:mm:ss
formatted date string fully browser compatible you would have to replace dashes with slashes:
var dateString = "2010-08-17 12:09:36"; new Date(dateString.replace(/-/g, "/"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With