0

I have two variables, date & time...

var date = "2012-12-05";
var time = "18:00";

How can I format this into a UTC formatted date. This is so I can use it within the Facebook API..

Facebook states I need it in this format:

Precise-time (e.g., '2012-07-04T19:00:00-0700'): events that start at a particular point in time, in a specific offset from UTC. This is the way new Facebook events keep track of time, and allows users to view events in different timezones.

Any help would be much appreciated.. Thanks!

1

2 Answers 2

1

This format is called ISO 8601

Do you know what timezone you are in? If you do, you can do like this:

var datetime = date + 'T' + time + ":00+0000';

if the timezone is +0.
if not, then:

var d = new Date()
var n = d.getTimezoneOffset();
var timeZone = Math.floor( Math.abs( n/60 ) );
var timeZoneString = (d.getTimezoneOffset() < 0 ? '-' : '+' ) + ( timeZone < 10 ? '0' + timeZone : timeZone ) + '00';

var datetime = date + 'T' + time + ':00' + timeZoneString;

Here is a fiddle: http://jsfiddle.net/ajySr/

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

2 Comments

Is there any way to automatically get the timezone?
There is the getTimezoneOffset function: developer.mozilla.org/en-US/docs/JavaScript/Reference/…
1

Try the following:

var date = new Date(2012, 12, 5, 18, 0, 0, 0); 
var date_utc = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(),  date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());

The Date function can be used the following ways:

var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

This was taken from a related post here: How do you convert a JavaScript date to UTC?

To find out more, please refer to: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date

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.