0

I have the following array:

1366004070000,1,1366004406000,1

The first and third entries are a unix timestamp. I need to convert this into a local datetime. How would I create a function to do this:

Here is what non-functional structure I have so far:

function toLocalTime(data) {
    for (unixTime, successful in data) {
        var d = new Date();
        var tzOffset = d.getTimezoneOffset();
        var new_time = unixTime - tzOffset
    ??
   };
   return newArray
}

I would like to produce an array of the same structure as the above, but with the timestamp converted to the localtime, something like:

1366004070444,1,1366004406444,1

Update: not one of the below answers gets the data in the format that I'm looking for. I am not looking for a local time object. I am looking for an array that gives me an "adjusted unix timestamp". In other words, the output should look indentical to the input, except for the slight adjustment in time from utc to localtime.

David542
  • 104,438
  • 178
  • 489
  • 842

5 Answers5

1

You ask to convert the array times to local times - so this is the correct answer (rest of the answers compare it to today)

http://jsfiddle.net/vWKYW/

var times = [1366004070000,1,1366004406000,1];
var local_times = [];
for(var i=0;i<times.length;i++){
     var time = times[i];
    if(time != 1){
        var d = new Date(time);
        var tz = d.getTimezoneOffset();        
        var tz_ms = tz*60*1000;
        time = d.getTime() - tz_ms;
    }

    local_times.push(time);
}

console.log(local_times);
Adidi
  • 5,097
  • 4
  • 23
  • 30
0

It seems like you want this:

var arr = [1366004070000,1,1366004406000,1];
for (var i=0; i<arr.length; i+=2) {
    var unixTime = arr[i],
        successful = arr[i+1];

    var date = new Date(unixTime);
    console.log(date.toString());  // local: Mon Apr 15 2013 07:34:30 GMT+0200
    console.log(date.toUTCString()); // UTC: Mon, 15 Apr 2013 05:34:30 GMT
}

Since the timestamps are always counted from UTC, you won't need to convert anything. Just use the non-UTC output methods (like getHour() instead of getUTCHour()) on your Date objects.


While I don't see what you would do with "local-timezone adjusted timestamps" in JavaScript, this would be the code to create them:

function toLocalTime(data) {
    var newArray = [];
    for (var i=0; i<data.length; i+=2) {
        var unixTime = data[i];
        var d = new Date(data[i]);
        var tzOffset = d.getTimezoneOffset() * 6e4;
        newArray[i] = unixTime - tzOffset;
        newArray[i+1] = data[i+1];
   }
   return newArray;
}

getTimezoneOffset did return minutes, not milliseconds.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • what is the array that I return here in the function? – David542 Apr 15 '13 at 00:09
  • I did suggest not to change anything with the timestamps, so I didn't return anything :-) Yet you could assign the date objects to the array for example (`arr[i] = date;`) and return the mutated `arr` then. – Bergi Apr 15 '13 at 00:12
  • thanks, please see updated question with an emphasis on what I'm looking to get. – David542 Apr 15 '13 at 00:24
  • Also, with the current function you have, every single console prints "invalid date". – David542 Apr 15 '13 at 00:30
  • Does it? Not if the array elements are numbers - that's not really clear from you question. – Bergi Apr 15 '13 at 00:39
0

if I understand correctly ( i don't think ) what you need is:

function toLocalTime(data) {
    data[0]  = getLocalTime(data[0]);
    data[2]  = getLocalTime(data[2]);
    return data;
   };
}

function getLocalTime(unixTime){
   var d = new Date();
   var tzOffset = d.getTimezoneOffset()*60000; // As @Bergi suggest :) 
   return unixTime - tzOffset;
}
Jayyrus
  • 12,961
  • 41
  • 132
  • 214
0

Yoy may want to use a regular for loop instead:

var unixTimes = [1366004070000,1,1366004406000,1];

function toLocalTime(data) {
    var localTimes = [];
    for (var i = 0; i < data.length; i++) {
        var d = new Date();
        var tzOffset = d.getTimezoneOffset() * 60 * 1000; //To convert to ms
        var new_time = data[i] - tzOffset;
        localTimes.push(new_time);
   };
   return localTimes;
}

var newTimes = toLocalTime(unixTimes);
ricardohdz
  • 579
  • 4
  • 9
0

This is what worked:

for(var i=0; i<data.length; i++) {
 29         var d = new Date(parseInt(data[i][0]));
 30         var tz = d.getTimezoneOffset();
 31         var ms = tz * 60 * 1000;
 32         data[i][0] = d.getTime() - ms;
 33         //console.log(data[i][0]);
 34     }
David542
  • 104,438
  • 178
  • 489
  • 842