我有一个简单的文本框,我将输入一个随机日期(8/6/2017)。我有三个标签。我想要的只是label1.text = 1/1/2017和label2.text = 7/1/2017和label3.text = 7/31/2017。
必须根据我在文本框中输入的日期拉出这些日期,并且不限于当前年份。
将1stDayYr设置为新日期(日期。“”&Textbox.text&“” .Year,1,1)
Label1.text = 1stDayYr
TBH-我什至不知道如何开始基于TB日期的上个月的第一天或根据TB日期的上个月的最后一天。
答案 0 :(得分:0)
Dim baseDate As DateTime
Dim beginningOfYear As DateTime
Dim beginningOfPreviousMonth As DateTime
Dim endOfPreviousMonth As DateTime
If DateTime.TryParse(TextBox1.Text, baseDate) Then
beginningOfYear = New DateTime(Year(baseDate), 1, 1)
Dim previous As DateTime = baseDate.AddMonths(-1)
beginningOfPreviousMonth = baseDate.AddMonths(-1).AddDays(-1 * previous.Day).AddDays(1)
endOfPreviousMonth = beginningOfPreviousMonth.AddMonths(1).AddDays(-1)
Console.WriteLine("Base date: {0} Beginning of year: {1} Beginning of previous month: {2} End of previous month: {3}", baseDate, beginningOfYear, beginningOfPreviousMonth, endOfPreviousMonth)
Else
Console.WriteLine("Invalid date")
End If
但是您应该将DateTimePicker用于日期输入
答案 1 :(得分:0)
感谢您的回答,我很接近。
Dim previous As DateTime = baseDate.AddMonths(-1)
beginningOfPreviousMonth = baseDate.AddMonths(-1).AddDays(-1 * previous.Day).AddDays(1)
VS不喜欢您以前的一天。我也不知道为什么。