...

View Full Version : Resolved Javascript Time



embeebutterly
07-19-2012, 01:36 PM
hi guys. i need something that can work out what date and time im getting from a 3rd party site. The way it comes to me is like this: 1342461600

It will have to work out a formula based around the fact that i know the following:

I know that is 1342461600 = 19:00 16th July 2012
I also know that 1342548000 = 19:00 17th July 2012

I know that the difference is 86400 seconds which is 1440 minutes which is 24 hours. As you can see, the number grows every second by 1

i know basicly that the question/formula is if 1342461600 = 19:00 16th July 2012 what does 1342548000 = ???

but i dont have a clue how to write it!!

So when i recieve the string in the url like &date=1342461600 and can place it within your code like this :
'+date+' i need to convert it and print it in the correct format. I would prefer if i can break it up to print it seperately like



'+hour+'
'+min+'
'+day+'
'+month+'
'+year+'


Dont worry about daylight savings, thats managed at source or i can reset the formula if its not.

If its not too much to ask it would be nice if it could handle 'st' , 'nd' , 'rd' and 'th' too eg: 1st, 2nd, 4th
maybe:
if(day == "1"){
document.write(''+day+'st');
}else if(day == "2"){
document.include(''+day+'nd');
}
And so on
Tho im sure thats quite alot more long winded than it needs to be!



it would also need to know that 30 days have september, april, june and november etc!!


Anyone up to the challange?

Cheers Guys

Philip M
07-19-2012, 02:16 PM
This is a well-trodden path. You ought to study the Javascript Date object.


<script type = "text/javascript">

var secs = 1342461600; // a value in seconds - Javascript epoch time since 1/1/1970 GMT
var ms = secs * 1000; // make milliseconds
var d = new Date(ms); // create a new Date object
var year = d.getFullYear();
var month = d.getMonth();
var date = d.getDate();
var hrs = d.getHours();
if (hrs <10) {hrs = "0" + hrs}
var mins = d.getMinutes();
if (mins <10) {mins = "0" + mins}
var day = d.getDay();
var mth=['January','February','March','April','May','June','July','August','September','October','November',' December'];
var dy = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
suffix = (date==1 || date==21 || date==31) ? "st" : "th" && (date==2 || date==22) ? "nd" : "th" && (date==3 || date==23) ? "rd" : "th";
alert (hrs + ":" + mins + " " + dy[day] + "," + " " + date + suffix + " " + mth[month] + "," + " " + year);
document.write(hrs + ":" + mins + " " + dy[day] + "," + " " + date + suffix + " " + mth[month] + "," + " " + year);


</script>

Obviously you can format the output to meet your precise requirements.

If you are 20 and you are not a socialist, then you have no heart. If you are 40 and you are still a socialist, then you have no brain.

embeebutterly
07-19-2012, 03:32 PM
Thank you very much, this is perfect! yes its true i have alot to learn. ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum