简体   繁体   English

Windows Phone 7.5中C#文本框中的日期掩码

[英]Date Mask in textbox in windows phone 7.5 in C#

In windows phone 7.5 there is no date control ,I want to use date mask in text box and also validate false if user input wrong date . 在Windows Phone 7.5中没有日期控件,我想在文本框中使用日期掩码,并且如果用户输入错误的日期也要验证false。 Please help me. 请帮我。

Thanks in Advance. 提前致谢。

您可以使用DateTimePicker ,因此用户将只能选择有效日期。

you could create a DateTime variable like this 您可以像这样创建一个DateTime变量

DateTime myValue = DateTime.Now;
myTextBlock.Text = myValue.ToString();

from here you could format it to the following if you like 从这里可以将其格式化为以下格式

Now what if you wanted to display only the Date and exclude the time? 现在,如果您只想显示日期而不包括时间怎么办? Luckily there are a number of methods available in the DateTime class which will enable you to display the date and/or time in a specific format. 幸运的是,DateTime类中提供了许多方法,这些方法使您能够以特定格式显示日期和/或时间。 Let's have a look at these methods and the output they generate. 让我们看一下这些方法及其生成的输出。

  1. myTextBlock.Text = myValue.ToShortDateString(); myTextBlock.Text = myValue.ToShortDateString();

This statement displays only the Date in a short date format,ie, dd/mm/yyyy or mm/dd/yyyy depending upon phone's regional date time settings. 此语句仅显示短日期格式的日期,即dd / mm / yyyy或mm / dd / yyyy,具体取决于电话的区域日期时间设置。 Eg 03/01/2012 例如2012年3月1日

  1. myTextBlock.Text = myValue.ToShortTimeString(); myTextBlock.Text = myValue.ToShortTimeString();

In this statement we use the ToShortTimeString() method to display only the time. 在此语句中,我们使用ToShortTimeString()方法仅显示时间。 Eg 1:06 PM 例如:下午1:06

  1. myTextBlock.Text = myValue.ToLongDateString(); myTextBlock.Text = myValue.ToLongDateString();

In this statement the ToLongDateString() method displays the Day of the week followed by the Date in numbers, the Month in alphabets and the year in numbers. 在此语句中,ToLongDateString()方法显示星期几,其后是数字的日期,以字母的月份和以数字的年份。 Eg Thursday, March 01, 2012. 例如,2012年3月1日,星期四。

use this link as a reference Working with DateTime Windows Phone 7.5 使用此链接作为参考使用DateTime Windows Phone 7.5

http://msdn.microsoft.com/en-us/library/ch92fbc1(v=vs.95).aspx http://msdn.microsoft.com/en-us/library/ch92fbc1(v=vs.95).aspx

Try use TryParse method to know if string written to textbox can be converted to DateTime, if true then its valid. 尝试使用TryParse方法来了解写入文本框的字符串是否可以转换为DateTime,如果为true,则其有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM