3

I have been reading up on dates for days, seemingly going in circles here. I have a string in a DB that looks like this

2012,03,13,01,31,38 

I want to create a js date object from it so...

new Date(2012,03,13,01,31,38); 

Easy enough, right? But it comes back as

2012-04-13 05:31:38 +0000 

So the month is off by 1 and the time is off by 4 hours (maybe DST or Timezone related???). I simply want a date that matches the one I provided. Its driving me nuts, dealing with these JS date objects.

How can I be sure the date object is the exact same date and time as the string suggests, I have no need for Timezone or DST changes, simply a date that matches a string.

More specifics regarding application: My application for this need is for an iphone app I am developing in Titanium (which builds using JS). Basically, part of my app involves logging data and with that log I collect the device's current date and time. I save all of this information to a mySQL database. The field in the database looks like this format: "2012-02-16 00:12:32"

Here is where I start to run into problems. I am now offering the ability to edit the log, including the date and time it was logged. In order to use an iPhone "picker", I must convert the string above into a JS date object again. This usually screws things up for me. I essentially need to create a new date object with the date above, with timezone and dst being completely irrelevant, so that when I save back to the DB, its just the string above, modified as per the users request. It needs to not matter whether they are editing in pennsylvania or china, they are editing the same log date.

Sorry if this has been confusing. I am having a hard time figuring out this whole date stuff.

2
  • A Date object has more than one toString() method. Just use the output format which was implicitly used for your input, and you won't get into timezone troubles. Commented Feb 18, 2012 at 4:07
  • How are you trying to print out this string? Just typing in new Date(...) in the console works as expected in both Firefox and IE9 - it displays something like "Fri Apr 13 2012 01:31:38". Commented Feb 18, 2012 at 4:31

2 Answers 2

2

This depends on what your string is. If that string is UTC time, you need to parse it as that. If it's local time, you need to parse it as local time. You can make a helper method like this for that part:

function getDate(utc, year, month, day, hour, minute, second) { if(utc) { var utc = Date.UTC(year, month - 1, day, hour, minute, second); return new Date(utc); } else { return new Date(year, month - 1, day, hour, minute, second); } } 

Now, to parse your string, you can use this:

function fromString(utc, str) { var parts = str.split(','); var year = parts[0]; var month = parts[1]; var day = parts[2]; var hour = parts[3]; var minute = parts[4]; var second = parts[5]; return getDate(utc, year, month, day, hour, minute, second); } 

which you can use like this for your example:

var d = fromString(true, '2012,02,13,00,31,38'); // If UTC var d = fromString(false, '2012,02,13,00,31,38'); // If local time 

Here's a working jsFiddle that you can play with:

which also shows two ways to print the date (UTC or local). Hope this helps.

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

4 Comments

That fiddle is helpful, I think I may be able to work that into something useful. I've added more specifics to my application of this need in my post.
Given the additional info, what you want is to use some fixed time zone on the server, UTC is probably the best, which you have above. You generally want to take the string back from the server, which will be UTC, parse it as UTC as above, do all the edits as necessary in local time on the client, then convert back to UTC just before you send it to server.
I will check it out tonight and see if I can get that combo to work for me. Thanks so far for the help!
I was able to use your provided code to convert the string to a correct date object, edit the date object with an iPhone picker, and save back to the db as a string. Exactly what I needed. Thank you!
0

I had the same problem. There are two reasons for the weird time change:

  1. Use new Date(Date.UTC(2012,03,13,01,31,38)) to avoid the time change.
  2. Note that the month is zero based! Months go from 0 to 11 for this function.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.