简体   繁体   English

从给定日期获取日期

[英]Get day from the given date

Using VB.Net 2005 使用VB.Net 2005

I want to get a day for the date. 我想约会的日子。

My Datepicker format is Custom Format (dd/MM/yyyy) 我的日期选择器格式是自定义格式(dd / MM / yyyy)

Code. 码。

Dim dt As New DateTime()
dt = Format(textdate.Text, "dd/MM/yyyy")
textday.Text = dt.DayOfWeek.ToString

When i try to run the above code, it was taking only this format(MM/dd/yyyy) 当我尝试运行上面的代码时,它仅采用这种格式(MM / dd / yyyy)

Suppose 假设

Date - 02/05/2010 日期-2010年2月5日

It should display as "Tuesday of Feburary" but it is displaying as "Sunday of May" 它应显示为“ February Tuesday”,但应显示为“ MaySunday”

Need VB.Net code Help 需要VB.Net代码帮助

You should have a look at 你应该看看

DateTime.TryParseExact Method (String, array [] () [], IFormatProvider, DateTimeStyles, DateTime) DateTime.TryParseExact方法(字符串,数组[]()[],IFormatProvider,DateTimeStyles,DateTime)

It should be something similar to 它应该类似于

Dim dt As New DateTime()
DateTime.TryParseExact("02/05/2010", "MM/dd/yyyy", Nothing, Globalization.DateTimeStyles.None, dt)

If you're using a DateTimePicker , you shouldn't need to do any formatting in the first place. 如果您使用的是DateTimePicker ,则无需首先进行任何格式化。 Just use DateTimePicker.Value to get the value as a DateTime . 只需使用DateTimePicker.Value即可将值获取为DateTime

I don't know what Format does used like that - I'd have expected a call to DateTime.Parse/ParseExact/TryParse/TryParseExact , as per astander's answer - but it would still be better if you didn't do any parsing at all, and just asked the control for the value. 我不知道Format那样使用了什么-根据期待的回答,我曾期望调用DateTime.Parse/ParseExact/TryParse/TryParseExact但如果您不进行任何解析,那还是更好的所有,只是问控件的价值。

date format always depend on the regional setting of the current computer, in your example the instance where this code run expect mm/dd/yyyy 日期格式始终取决于当前计算机的区域设置,在您的示例中,此代码运行的实例预期为mm / dd / yyyy

to fix this, you need to change the regional setting of the application 要解决此问题,您需要更改应用程序的区域设置

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

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