Date formatting?
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)
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
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.
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() );
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
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>
精彩评论