3

I'm pulling some data from two different APIs and I want to the objects later on.

However, I'm getting two different date formats: this format "1427457730" and this format "2015-04-10T09:12:22Z". How can I change the format of one of these so I have the same format to work with?

$.each(object, function(index) {
  date = object[index].updated_at;
}
1
  • Load es5-shim to ensure that the environment handles toISOString then date = (/^\d+$/).test(d) ? new Date(d * 1000).toISOString().slice(0, -5) + 'Z': d; where d is object[index].updated_at Now all formats will be like "2015-04-10T09:12:22Z" Commented Jan 9, 2016 at 16:27

4 Answers 4

4

Here's one option:

var timestamp  = 1427457730;
var date       = new Date(timestamp * 1000); // wants milliseconds, not seconds
var dateString = date.toISOString().replace(/\.\d+Z/, 'Z'); // remove the ms

dateString will now be 2015-03-27T12:02:10Z.

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

1 Comment

This worked for me. if got the same formatting perfectly thanks!
3

Try moment.js

var timestamp = 1427457730;
var date      = '2015-04-10T09:12:22Z';

var m1 = moment(timestamp);
var m2 = moment(date);

console.log(m1);
console.log(m2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.1/moment.min.js"></script>

You can use .format() method in moment to parse the date to whatever format you want, just like:

m2.format('YYYY MMM DD ddd HH:mm:ss') // 2015 Apr 10 Fri 17:12:22

Check out the docs for more format tokens.

Comments

2

What you probably want in javascript, are date objects.
The first string is seconds since epoch, javascript needs milliseconds, so multiply it by 1000;
The second string is a valid ISO date, so if the string contains a hyphen just pass it into new Date.

var date = returned_date.indexOf('-') !== -1 ? returned_date : returned_date * 1000;

var date_object = new Date(date);

Making both types into date objects, you could even turn that into a handy function

function format_date(date) {
    return new Date(date.indexOf('-') !== -1 ? date : date * 1000);
}

FIDDLE

5 Comments

This is also a great way of solving it. what would be easier to work with, this way? or the method of @robertklep? In the future I would like to order objects chronological based on these dates
What's easier depends on you I suppose, this takes both types, and returns objects, which you then could stick in an array, and just sort them
I tried to sort it today, but it won't sort. I'm using .sort(); on the array. I sorts nicely if I fill the array with something else like strings. dl.dropboxusercontent.com/u/1207224/… any suggestions on sorting with the function you wrote?
@user3199999 - sorting dates should work just fine -> jsfiddle.net/fg9fvqt6/1
thanks man, I first tried it without the function in the sort. thanks for the tip. I'm not a expert in adding those minimalistic functions, with the sort of function you made for sorting. Now A and B are a item in the array. I my case I'm using a array filled with arrays which have a timestamp on the first place like so -> jsfiddle.net/fg9fvqt6/2
2

Take a look at http://momentjs.com/. It is THE date/time formatting library for JavaScript - very simple to use, extremely flexible.

1 Comment

thanks for the tip, though if possible I would like to not use additional plugins since this is the only date/time stuff I'm working with

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.