Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
90
rated 0 times [  96] [ 6]  / answers: 1 / hits: 44748  / 13 Years ago, fri, september 30, 2011, 12:00:00

I am currently having some issues converting a string dateTime object in JavaScript



I am assuming it is because my string cannot me used properly in a new Date() but I'm not sure that is the problem.



My Input: 2011-09-29 14:58:12



My code:



var date = 2011-09-29 14:58:12;
var added = new Date(date);
var year = added.getYear();


However, my year var contains NaN. Same with getDay() or getMonth(). What is the problem?



ps: I'm getting the date in it's format from a SQLite database. And I'm using Titanium Mobile, so javascript and SQLite are the only things involved


More From » datetime

 Answers
31

You're relying on the Date constructor parsing an unsupported format. Until recently, there was no standard string format supported by the Date constructor. As of ECMAScript5, there is one (YYYY-MM-DDTHH:MM:SS, note the T rather than space), but it's only been specified for just under two years and naturally doesn't work in older browsers.


For the time being, your best bet is to parse it yourself (you can find code in this question and its answers), or use something like DateJS, MomentJS, date-fns, etc. to parse it for you.


[#89845] Wednesday, September 28, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jadyngraysons

Total Points: 455
Total Questions: 109
Total Answers: 98

Location: Trinidad and Tobago
Member since Fri, May 8, 2020
4 Years ago
jadyngraysons questions
Thu, Apr 23, 20, 00:00, 4 Years ago
Sat, Jan 18, 20, 00:00, 4 Years ago
Tue, Dec 31, 19, 00:00, 4 Years ago
;