PDA

View Full Version : exist any javascript function checks a date if is correct



leonidassavvides
10-27-2008, 09:31 AM
exist any javascript function checks a date if is correct
eg a wrong date= 31 feb 2009, returns false ?

To show local time(NOW) GMT+2 to each visitor indepented where he is[time zone] I must use server time zone or exist and js function gives GMT TIME and after I add two hours to this ?
this is corect
var today2gmt = Date.UTC()+2*60*60;
// ms today GMT+2 TIME ANYWHERE

This builds an object: today2gmt = Date.UTC()+2*60*60 or not I must use
gmt2 - new Date(today2gmt); // this is corect ?

To asign to a day/month/year html form fields (date) the current date I must use if FormName="book" and day/month/year field names are "day","month","year" and I have a date object "gmt2"
document.book.day.value = gmt2.getDay();
document.book.month.value = months[gmt2.getMonth()]
document.book.year.value = gmt2.getFullYear();
when I must call these statements , when page loads at head section ?

Jesdisciple
10-28-2008, 07:08 PM
exist any javascript function checks a date if is correct
eg a wrong date= 31 feb 2009, returns false ?One does now. :) When you set an out-of-bounds date, it will "roll over" to the best guess at what you meant.

function dateExists(date, month, year){
var d = new Date(year, month, date);
return d.getDate() === parseInt(date); //parseInt makes sure it's an integer.
}
//January 1, 2009
document.write(dateExists(1, 0, 2009) + '<br>'); //true
//February 31, 2009
document.write(dateExists(31, 1, 2009) + '<br>'); //false - rolls over to March 3, 2009; 3 !== 31


To show local time(NOW) GMT+2 to each visitor indepented where he is[time zone] I must use server time zone or exist and js function gives GMT TIME and after I add two hours to this ?
this is corect
var today2gmt = Date.UTC()+2*60*60;
// ms today GMT+2 TIME ANYWHERE

This builds an object: today2gmt = Date.UTC()+2*60*60 or not I must use
gmt2 - new Date(today2gmt); // this is corect ?That's incorrect; Date.UTC requires at least two (three in IE) arguments (ECMA (http://interglacial.com/javascript_spec/a-15.html#a-15.9.4.3), Mozilla (http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date/UTC), Microsoft (http://msdn.microsoft.com/en-us/library/wz6stk2z(VS.85).aspx)). But this works:

var d = new Date();
d.setHours(d.getUTCHours() + 2);(Note that d.getUTCHours will be unreliable after this line, depending on the user's timezone.)


To asign to a day/month/year html form fields (date) the current date I must use if FormName="book" and day/month/year field names are "day","month","year" and I have a date object "gmt2"
document.book.day.value = gmt2.getDay();
document.book.month.value = months[gmt2.getMonth()]
document.book.year.value = gmt2.getFullYear();
when I must call these statements , when page loads at head section ?This is a more reliable way:
document.forms.book.day.value = gmt2.getDay();
document.forms.book.month.value = months[gmt2.getMonth()]
document.forms.book.year.value = gmt2.getFullYear();This must be done either here:
<body>
Put all your page's contents above the script tag.
<script type="text/javascript">//Move this script to its own file.
...
</script>
</body>or here:
<head>
<script type="text/javascript">//Move this script to its own file.
window.onload = function(){
...
}
</script>
</head>