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

Cute_Ninja
  • 4,742
  • 4
  • 39
  • 63
  • @janaspage: How can I fix that? I do not want that time to get off by hours or even minutes. Can I achieve that? – Cute_Ninja Jun 24 '14 at 21:21
  • Please show us the `.toUTC()` function you use, because it is that function that does it! – André Snede Jun 24 '14 at 21:40
  • *sigh* - this is about the billionth question of this sort. The simple answer is to use [moment.js](http://momentjs.com). The harder answer is that hyphens trigger the date object to use UTC. For example, [see here](http://stackoverflow.com/a/24070550/634824) or [here](http://stackoverflow.com/q/18774293/634824). – Matt Johnson-Pint Jun 24 '14 at 21:49
  • @MattJohnson - That is not the issue here. – André Snede Jun 24 '14 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. – Matt Johnson-Pint Jun 24 '14 at 21:55

3 Answers3

2

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.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
1

Try using the following:

new Date(new_d); 
khalid13
  • 2,767
  • 2
  • 30
  • 48
  • 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 – Cute_Ninja Jun 24 '14 at 20:41
1

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:

André Snede
  • 9,899
  • 7
  • 43
  • 67
  • 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? – Cute_Ninja Jun 24 '14 at 21:05
  • 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. – André Snede Jun 24 '14 at 21:35
  • 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 – Cute_Ninja Jun 24 '14 at 21:59
  • Nope it's your `toUTC()` that is breaking it. – André Snede Jun 24 '14 at 22:00
  • Nope, it is not. I tested – Cute_Ninja Jun 24 '14 at 22:00