日期时间值问题 [英] datetime value problem

查看:94
本文介绍了日期时间值问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的先生/女士



i我在使用网络应用时从文本框中取出日期。



但我发现的问题是

String未被识别为有效的DateTime。





i我正在使用

  Dim  dt1 作为 DateTime 
dt1 = DateTime.ParseExact(Convert.ToDateTime( Me .txt_frmdate.Text), MM / dd / yyyy,System.Globalization.CultureInfo.InvariantCulture)







这里txt_frmdate是一个文本框,我在其中选择日历扩展器的日期

解决方案

除了解决方案1:< br $> b $ b

  1. 查看结构的其他方法 System.DateTime Parse ParseExact TryParse TryParse完全,所有这些:

    http ://msdn.microsoft.com/en-us/library/system.datetime.aspx [ ^ ]。



    有不同的签名,允许选择精确的格式字符串,文化或两者兼而有之,以Try开头的方法不会抛出异常,但在成功的情况下返回布尔值。每种方法的每个MSDN帮助页面都附带一个清晰的代码示例。
  2. 学习格式说明符:

    http://msdn.microsoft.com/en-us/library/az4se3k1.aspx [ ^ ],

    http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx [ ^ ]。





此外,时间使用文本输入并不是那么好。我建议改为使用 DateTimePicker

http://www.codeproject.com/KB/webforms/DateTimePicker.aspx [ ^ ]。



-SA


参考 - DateTime.ParseExact方法(字符串,字符串,IFormatProvider) [ ^ ]。

< blockquote class =FQ>

Quote:

使用指定的格式和特定​​于文化的格式将指定的日期和时间字符串表示形式转换为其DateTime等效项格式信息。字符串表示的格式必须与指定的格式完全匹配。



但是你已经将字符串转换为日期,然后在 ParseExact 函数作为第一个参数。



所以,试试如下...

 Dim dt1 As DateTime 
dt1 = DateTime.ParseExact(Me.txt_frmdate.Text, MM / dd / yyyy ,System.Globalization.CultureInfo.InvariantCulture)


Dear sir/ma'am

i am taking date from a text box while i am using webapplication.

but problem i found as
String was not recognized as a valid DateTime.


i am using as

Dim dt1 As DateTime
dt1 = DateTime.ParseExact(Convert.ToDateTime(Me.txt_frmdate.Text), "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)




here txt_frmdate is a text box in which i choose the date from calender extender

解决方案

In addition to Solution 1:

  1. Look at other method of the structure System.DateTime: Parse, ParseExact, TryParse and TryParseExact, all of them:
    http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

    The have different signatures and allow to choose either exact format string, culture, or both, the method started with "Try" don't throw exception, but return Boolean value in case of success. Each of the MSDN help pages on each method comes with a clear code sample.
  2. Learn format specifiers:
    http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
    http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].



Besides, it's not so good to use text input for time. I would advise to use DateTimePicker instead:
http://www.codeproject.com/KB/webforms/DateTimePicker.aspx[^].

—SA


Refer - DateTime.ParseExact Method (String, String, IFormatProvider)[^].

Quote:

Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.


But you have converted the string to date and then passed it inside this ParseExact Function as the first parameter.

So, try like below...

Dim dt1 As DateTime
dt1 = DateTime.ParseExact(Me.txt_frmdate.Text, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)


这篇关于日期时间值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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