1

My app will be run in Mexico and Australia.

When a user selects a date I format it as; "Friday 17 August 2012"

Now, how do I convert that string value into a UTC date?

So far I have this;

var enteredDate = $('.CreateDateRange').val() + " 07:00:00";
var thisDate = new Date(enteredDate);
var utcDate = new Date(thisDate.getUTCFullYear(), thisDate.getUTCMonth(), thisDate.getUTCDay(), thisDate.getUTCHours(), 0, 0);

But on the above date the returned date is; 2012 7 4 21:0:0 which is not correct.

From Australia I expect the UTC date to be something like "2012 8 17" or "2012 8 16" depending on time of day.

2
  • Did you try toUTCString ? Please try enteredDate.toString() and enteredDate.toUTCString() and check the timezone offsets. See the edited answer. Commented Aug 17, 2012 at 4:48
  • Don't use the browser location; just follow its time zone. Someone's computer might be set to his home time zone when traveling. Commented Aug 17, 2012 at 5:32

1 Answer 1

1

Find the UTC specific methods in Date object

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date

getUTCDate, getUTCDay, getUTCFullYear, getUTCHours, getUTCMilliseconds, getUTCMinutes, getUTCMonth, getUTCSeconds, toUTCString

EDIT:

toUTCString will give you what you need

var enteredDate = $('.CreateDateRange').val() + " 07:00:00";
var thisDate = new Date(enteredDate);
console.log(thisDate.toUTCString());
Sign up to request clarification or add additional context in comments.

Comments

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.