PDA

View Full Version : Strange behaviour when checking dates



cash1981
Jun 27th, 2007, 10:35 AM
Hello.
I have a form, and onchange of a textbox I start a javascript function which checks the date.

The first thing I want to do is check to see if the date is in correct format yyyy-MM-dd (2007-06-27)

The date which the user is going to input should be the tomorrows date, 2007-06-08

Now I have three problems.

The first problem is that the month is not 05 but 5. How can I make that kind of format on the date. I would like something like this:

currentTime = new Date("yyyy-MM-dd")
But this doesnt work.

The second problem is that my regex doesnt work. It always fails.

The third problem is that variable month returns wrong month!?!?!
currentTime is correct. It returns Jun 27 ....
But when I do currentTime.getMonth it returns 5, which is may. How strange is that??

I have put alerts to check the values.
Anybody have any ideas why this doesn't work?
Any help is greatly appreciative.

This is my script:


<script type="text/javascript">
<!--
function checkDate() {
if (PaperIssueForm.issueDate.value != "") {
var strStart = document.PaperIssueForm.issueDate.value;
var tomatch = /^\d{4}-d{2}-d{2}/
if (!tomatch.test(strStart)) {
alert("The value should be in the format yyyy-mm-dd");
}
}
var currentTime = new Date()
alert("Current time is: " + currentTime)
var month = currentTime.getMonth()
var day = currentTime.getDate() + 1
var year = currentTime.getFullYear()
var check = year + "-" + month + "-" + day
alert("Checking check: " + check + " against: " + document.PaperIssueForm.issueDate.value)
if(check == document.PaperIssueForm.issueDate.value) {
//do nothing
} else {
alert("The date is different from tomorrows date, are you sure?");
}
}
//-->
</script>

bhakti_thakkar
Jun 27th, 2007, 10:49 AM
For your first problem you will have to use javascript string functions and leftpad a "0" to get the 05 as the month instead of just five.

for your third problem, in javascript, months index starts from 0 i.e for january its 0 , for feb it 1 and so on... so u will have to do currentTime.getMonth+1 to get the desired result

hope this help you a bit

cash1981
Jun 27th, 2007, 10:53 AM
For your first problem you will have to use javascript string functions and leftpad a "0" to get the 05 as the month instead of just five.

for your third problem, in javascript, months index starts from 0 i.e for january its 0 , for feb it 1 and so on... so u will have to do currentTime.getMonth+1 to get the desired result

hope this help you a bit

Thank you for your reply. I cant put a 0 on my first problem because what if the month is 10 or 11? Then suddenly it will be wrong. I need to somehow say that the format of the date should be yyyy-MM-dd

_Aerospace_Eng_
Jun 27th, 2007, 10:55 AM
Just do a check on the month.

if(month < 10)
{
month = '0'+month;
}
Then suddenly the month is right again.

cash1981
Jun 27th, 2007, 11:40 AM
Lol stupid me that I didn't think of that. Thanks Aerospace_Eng_
Now I only need to know why my regex fails.

cash1981
Jun 27th, 2007, 11:54 AM
I found out why my regex didnt work. I had forgotten to put '\' infront of the other two d's.
Thanks for you help guys.