Click here to Skip to main content
15,909,030 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i build acalculater that calculates the week and day of a pregnat women.
the input is in a text format
and i want it to be in a date format. but when i change it, it doesn't calculate

What I have tried:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
 
 
<title>Untitled Document 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<meta http-equiv="Content-Style-Type" content="text/css"> 
<meta http-equiv="Content-Script-Type" content="text/javascript"> 
 
function calculate(f){ 
var entry = f.newD.value.split('/');//splits input value; 
if((entry.length&lt;3)||(entry[0].length!=2)||(entry[1].length!=2)||(entry[2].length!=4)||(Number(entry[0]>31))||(Number(entry[1]>12))){//some limitative conditions 
alert('Plese input data in format dd/mm/yyyy !'); 
f.difD.value = ''; 
return false; 
} 
y=entry[2]*1; 
m=entry[1]*1-1;//sets months to default values i.e Jan = 0; Feb = 1; etc. 
d=entry[0]*1; 
var newDate = new Date(y,m,d);//sets the new date 
var today = new Date();//sets the today date 
dif = ((today-newDate)/7)-1;//difference in milliseconds 
dif = Math.floor(dif/1000/60/60/24);//difference in days
yamim1 =((((today-newDate)/ 7) / (24 * 3600 * 1000))-dif)*7;
yamim1 = Math.floor(yamim1);
if(dif&lt;0){//if the date is in the past alert('The chosen date is in the past. Please input a valid date!'); 
f.difD.value = ''; 
return false; 
} 
f.difD.value = ' שבוע '+ dif + '  +  '+ yamim1 + ' ימים ' ;//assign the difference 
} 
 
 
 
 
   
    
  <br> 
Posted
Updated 23-Feb-17 21:01pm
v3

Google is your friend...

1. if you want to convert to a date: javascript format text as date - Google[^]

2. If you need to mask the input: javascript date masked input - Google[^]
 
Share this answer
 
Never do that. You cannot reliably work on Date as string. Instead, use JavaScript Dates[^]. Refer to my previous answer[^].
Instead of letting user to enter free form date string, use a date picker[^] instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900