0

How can I convert a UTC time into proper date - time format using Javascript?

This is what I want to do

var d = new Date("2014-01-01");
var new_d = d.toUTC(); // 1388534400000
var old_d = function(new_d){
    // return "2014-01-01" // how can i get this? 
}

Now How, can i get orignal date - 2014-01-01 from 1388534400000?

****Also, Please note that when i do this --- new Date(1388534400000); it gives date 1 day less. That is, instead of giving Jan 01 2014, it gives Dec 31 2013. But, I want Jan 01 2014.****

Is there any method to do the opposite of toUTC() method?

// _________ For those whose toUTC() doesnt work

"toUTC" method works in console of my chrome See screen shot below

enter image description here

5
  • @janaspage: How can I fix that? I do not want that time to get off by hours or even minutes. Can I achieve that? Commented Jun 24, 2014 at 21:21
  • Please show us the .toUTC() function you use, because it is that function that does it! Commented Jun 24, 2014 at 21:40
  • sigh - this is about the billionth question of this sort. The simple answer is to use moment.js. The harder answer is that hyphens trigger the date object to use UTC. For example, see here or here. Commented Jun 24, 2014 at 21:49
  • @MattJohnson - That is not the issue here. Commented Jun 24, 2014 at 21:55
  • 1
    toUTC is nonstandard though. You must have it declared in some script that is loaded, or using some non-standard version of chrome. Commented Jun 24, 2014 at 21:55

3 Answers 3

3

When you pass a string containing hyphens to the Date constructor, it will treat that as UTC. And if you don't pass a time, it will consider it to be midnight. If you are in a time zone that is behind UTC (such as in most of the Americas), you will see the wrong local time conversion.

Here's a screenshot of my chrome dev console, so you can see what I mean

screenshot

If I pass slashes instead:

screenshot

Consider using moment.js - which will accept a format parameter that will help you avoid this issue.

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

Comments

2

The problem lies with the way you instantiate the Date. Javascript interpretes the hyphens as an utc date, and slashes as local dates.

Giving the results that mark Explains.

var utcDate = new Date('2014-01-01') // returns a UTC date
var localDate = new Date('2014/01/01'); // Returns local date

But to translate a date back to your starting point string, you can do the following.

function toDateString(utcMillis){
    var date = new Date(utcMillis);
    d = date.getDate();
    m = date.getMonth() +1;
    y = date.getFullYear();
    return y + '-' + addLeadingZero(m, 2) + '-' + addLeadingZero(d,2);
}

function addLeadingZero(n, length){
   n = n+'';
   if(n.length<length)
      return addLeadingZero('0'+n, length--);
   else
      return n;
}

If you find yourself with a UTC date, you can still do this:

function toUTCDateString(utcMillis){
    var date = new Date(utcMillis);
    d = date.getUTCDate();
    m = date.getUTCMonth() +1;
    y = date.getUTCFullYear();
    return y + '-' + addLeadingZero(m, 2) + '-' + addLeadingZero(d,2);
}

To play around with it, and see it for yourself, see this Fiddle:

5 Comments

This is great. However, I dont know why it returns 1 day less because new Date("2014-01-01").toUTC() gives 1388534400000. So toUTCDate(1388534400000) should give back 2014-01-01. instead it gives one day less. Not sure why that's happening?
Your function .toUTC() doesn't exist in the standard. So I can't tell you why your results differ. Make a fiddle show casing your problem.
However, new Date("2012-01-01") gives Sat Dec 31 2011 16:00:00 GMT-0800 (PST). So, it is not toUTC method but Date itself add some regional offset i guess
Nope it's your toUTC() that is breaking it.
Nope, it is not. I tested
1

Try using the following:

new Date(new_d); 

1 Comment

it returns this - Tue Dec 31 2013 16:00:00 GMT-0800 (PST) and I want 2014-01-01. Also, I dont know why it returns this date ---Dec 31 2013 , instead of this date --- Jan 1 2014

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.