2

I have two date variable separately like following

startDate is a Date instance with the value Tue Jul 17 2012 00:00:00 GMT+0530 (IST)

startTime is a String with the value "11:30 AM"

Now what I need is join of both above date & time, as a Date.

startDateTime = Tue Jul 17 2012 11:30:00 GMT+0530 (IST)

I tried

new Date(startDate + " " + startDate) but outputting invalid date.

Also tried the way shown on this post. But still not working.

7
  • Have you tried date.js javascript library? It can convert/parse a lot of date formats. Commented Jul 20, 2012 at 10:02
  • 1
    Are startDate and startTime strings? Commented Jul 20, 2012 at 10:03
  • 2
    @JonasT, or moment.js, which I prefer. Commented Jul 20, 2012 at 10:04
  • moment.js has the advantage of having been maintained in the last few years, which DateJS has not. Commented Jul 20, 2012 at 10:04
  • 1
    @RikeshShah: To improve a question, use the "edit" link underneath it. I've done that for you on this occasion. Commented Jul 20, 2012 at 10:07

3 Answers 3

4

You can readily parse startTime if it's in a clearly-defined format, then use setHours and setMinutes: Live example | source

var startDateTime;
var parts = /^(\d+):(\d+) (AM|PM)$/.exec(startTime);
if (parts) {
    hours = parseInt(parts[1], 10);
    minutes = parseInt(parts[2], 10);
    if (parts[3] === "PM" && hours !== 12) {
        hours += 12;
    }
    else if (parts[3] === "AM" && hours === 12) {
        hours = 0;
    }
    if (!isNaN(hours) && !isNaN(minutes)) {
        startDateTime = new Date(startDate.getTime());
        startDateTime.setHours(hours);
        startDateTime.setMinutes(minutes);
    }
}

...or something along those lines.

Note that key to this is the fact you've said startDate is a Date instance. The above assumes we're working within the timezone of the JavaScript environment, not across zones. If you were starting with a date string instead, and that string specified a timezone other than the JavaScript environment's timezone, which you were then converting into a Date via new Date("Tues Jul...."), then you'd have to be sure to adjust the resulting Date to use either the local time of the environment, or UTC; if you adjusted it to be UTC, you'd use setUTCHours and setUTCSeconds above instead of setHours and setSeconds. Again, this is only an issue if your starting point is a date string, and that string specifies a timezone different from the timezone in which the code above is running.

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

11 Comments

Thanks !! Working perfectly :)
@RikeshShah: You may have to adjust it slightly if the AM / PM might be in lower case, etc., but that's the gist. But do check out moment.js.
@Crowder - I will take of it. And the moment I can't add any js in existing source.
This code won't work because of the timezone. The timestamp which you have to add the hours & minutes is actually Jul 16 2012, 20:30:00 UTC
@Bergi: It's fine, setHours and setMinutes work within the timezone, and all Date objects in the same JavaScript execution environment share the same timezone ("local" time). If I'd used setUTCHours/setUTCMinutes, now that would be a problem. :-)
|
2

You can do This:

var theDate = new Date("Tue Jul 17 2012 00:00:00 GMT+0530 (IST)");
var theTime = "11:30 AM";
var hours = theTime .substr(0,2);
var minutes = theTime .substr(3,2);
var amOrPm = theTime .substr(6,2);
if (hours < 12 && "PM" == amOrPm) {
    hours = +hours + 12;
}
theDate.setHours(hours);
theDate.setMinutes(minutes);

1 Comment

This results in 2012-07-16T11:30:00, the correct timestamp would be 2012-07-17T07:00:00 (both UTC)
0

Try

new Date(startDate.toDateString() + " " + startTime)

This combines the date string from your Date object with the time string, and should give you a valid date. Note that this ignores the timezone you initially worked with, you might need to add " GMT+0530" again.

However, because your date string is already timezone-biased (Jul 16 2012, 20:30:00 UTC) it might be better to add them together, i.e. like new Date(+startDate + milliseconds):

var startDate = new Date("Tue Jul 17 2012 00:00:00 GMT+0530");
var startTime = "11:30 AM";
return new Date(+startDate + +new Date("1 1 1970 "+startTime))

1 Comment

Does it? Which values have you used, what string passed to the constructor? Works for me.

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.