views:

48

answers:

5

Hi guys

I am using an api with a json endpoint to grab some data.

the api returns dates that look like: Date(1288205847730) (so a basic javascript date object)

How do I convert that to Thu, 28 Oct


That is the generalized question that I could work from.

The actual question is I want to show Thu, 28 Oct format if there is more than 24 hours between now and that date,

if there is less I want to show 13 hours 8 minutes

(note: the times used are made up and do not correspond to the time used)

A: 

This is a standard UNIX timestamp with milliseconds, so JavaScript can process it directly:

var timestamp = 1288205847730;
var dateTime = new Date(timestamp); // => Wed Oct 27 2010 13:57:27 GMT-0500 (Central Daylight Time)

As for formatting it, I'd suggest using someone else's code since there's already plenty out there. Here's a Google search to get you started; the first few results should get you started.

Jordan
cheers, I am aware of this, my question mostly pertains to the formatting of the date once it is in the object.
Hailwood
I was hoping javascript would have something similar to php's date methods.
Hailwood
Hailwood; I updated my answer with some information on formatting dates. There are several good libraries/plugins out there that do this kind of formatting.
Jordan
Hailwood: Unfortunately JavaScript doesn't have any built-in date-formatting functions like PHP. Your best bet is to Google around for a library that does what you need: http://www.google.com/search?q=javascript+date+format
Jordan
A: 

Each implementation of JavaScript can differ. I recommending this article on Working with Dates. It describes a function for formatting dates into strings.

Example:

var current_date = new Date ( );

var month_names = new Array ( );
month_names[month_names.length] = "January";
month_names[month_names.length] = "February";
month_names[month_names.length] = "March";
month_names[month_names.length] = "April";
month_names[month_names.length] = "May";
month_names[month_names.length] = "June";
month_names[month_names.length] = "July";
month_names[month_names.length] = "August";
month_names[month_names.length] = "September";
month_names[month_names.length] = "October";
month_names[month_names.length] = "November";
month_names[month_names.length] = "December";

var day_names = new Array ( );
day_names[day_names.length] = "Sunday";
day_names[day_names.length] = "Monday";
day_names[day_names.length] = "Tuesday";
day_names[day_names.length] = "Wednesday";
day_names[day_names.length] = "Thursday";
day_names[day_names.length] = "Friday";
day_names[day_names.length] = "Saturday";

document.write ( day_names[current_date.getDay()] );
document.write ( ", " );
document.write ( month_names[current_date.getMonth()] );
document.write ( " " + current_date.getDate() );
document.write ( " " );
document.write ( " " + current_date.getFullYear() );
Jordan
have to give a massive LOL to how those arrays are initialised...should just use literal notation.. like month_names = [ "January", "February", "March", "...etc" ] ;
RueTheWhirled
@RueTheWhirled. I agree, I was just quoting and didn't feel like rewriting the example.
Jordan
@Jordan, Fair enuf, but felt the need to point out that's a really bad example of defining an array's values
RueTheWhirled
@RTW. No problem. I didn't even take offense.
Jordan
A: 

The best you can do without building the whole string yourself with plain javascript is

Date d = new Date(1288205847730); // milliseconds
d.toLocaleDateString(); 

If you want to use a library like jQuery you can find plugins that do it: http://plugins.jquery.com/project/jquery-dateFormat

bemace
A: 

Yeah unfortunately there isn't any decent built in date methods like php has. So short of using a library, which might be over kill, you'd have to create your own formatting function. Jordons on the right track but please don't initialise your arrays like that heh use the below

month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November","December"];

Here's a list of the available date functions http://www.w3schools.com/jsref/jsref_obj_date.asp

RueTheWhirled
A: 

Got this one out of my toolbox. You can mod the date obj's prototype to your specification.

<script type="text/javascript" language="javascript">
Date.prototype.toDayDateMonth = function(){
    var dayname = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
    var monthname = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    return ( dayname[this.getDay()] + " " + this.getDate() + " " + monthname[this.getMonth()] )
}

var d1 = new Date(1288205847730);
alert(d1.toDayDateMonth());
</script>
ajax81