我想要“输入日期”它采用文本格式,采用日期格式 [英] I want the "input date" that is in a text format to be in a date format

查看:127
本文介绍了我想要“输入日期”它采用文本格式,采用日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个计算孕妇周和日的acalculater。

输入是文本格式

我希望它是一个日期格式。但是当我改变它时,它不计算



我尝试过:



<!DOCTYPE HTML PUBLIC -  // W3C // DTD HTML 4.01 Transitional // EN> 


< title> Untitled Document
< meta http-equiv =Content-Typecontent =text / html; charset = iso-8859-1> ;
< meta http-equiv =Content-Style-Typecontent =text / css>
< meta http-equiv =Content-Script-Typecontent =text / javascript>

函数calculate(f){
var entry = f.newD.value.split('/'); //拆分输入值;
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))){//一些限制条件
alert('Plese input data in格式dd / mm / yyyy!');
f.difD.value ='';
返回false;
}
y =条目[2] * 1;
m = entry [1] * 1-1; //将月份设置为默认值,即Jan = 0; 2月= 1;等等
d =条目[0] * 1;
var newDate = new Date(y,m,d); //设置新日期
var today = new Date(); //设置今天日期
dif =(今天-newDate)/ 7)-1; //以毫秒为单位的差异
dif = Math.floor(dif / 1000/60/60/24); //天数差异
yamim1 =((((今天 - 新日期)/ 7)/(24 * 3600 * 1000)) - dif)* 7;
yamim1 = Math.floor(yamim1);
if(dif& lt; 0){//如果日期在过去的警报中('所选日期是过去。请输入有效日期!');
f.difD.value ='';
返回false;
}
f.difD.value ='שבוע'+ dif +'+'+ yamim1 +'ימים'; //分配差额
}






< br>

解决方案

Google是你的朋友......



1.如果您要转换为日期: javascript格式文字为日期 - Google [ ^ ]



2.如果您需要屏蔽输入: javascript date masked input - Google [ ^ ]


永远不要那样做。您不能可靠地将Date作为字符串处理。相反,请使用 JavaScript日期 [ ^ ]。请参阅我以前的回答 [ ^ ]。

不要让用户输入自由格式日期字符串,而是使用日期选择器 [< a href =https://jqueryui.com/datepicker/target =_ blanktitle =New Window> ^ ]。


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> 

解决方案

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[^]


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.


这篇关于我想要“输入日期”它采用文本格式,采用日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆