vb.net 如何自动将 datetimepicker 值设置为所选月份的第一天?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26645561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:25:26  来源:igfitidea点击:

How to set datetimepicker value to 1st of selected month automatically?

.netvb.netsql-server-2008datetime

提问by Indrah

when i check monthwise radiobtn the datetimepicker value should automatically set to 1st day of selected month.

当我按月检查 radiobtn 时,datetimepicker 值应自动设置为所选月份的第一天。

how to do this??

这该怎么做??

tried

试过了

dtpDate.value=new Date(1:MM:YYYY)

but not working as expected..

但没有按预期工作..

采纳答案by Nabeel Haxxan

You can try this :

你可以试试这个:

 dtpDate.Value = Now.Date.AddDays(-(Now.Day) + 1)

Date.AddDayswill add specified number of days to the date mentioned

Date.AddDays将指定的天数添加到提到的日期

Now.Daywill give the current day

Now.Day将给出当前日期

dtpDate.Value = Now.Date.AddDays(-(Now.Day) + 1)will always points to the first day of current month

dtpDate.Value = Now.Date.AddDays(-(Now.Day) + 1)将始终指向当月的第一天

Updates:set date to 01 of selected month on month change

更新:在月份更改时将日期设置为所选月份的 01

   Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
     DateTimePicker1.Value = CDate("01/" & DateTimePicker1.Value.Month & "/" & DateTimePicker1.Value.Year) 
   End Sub

回答by Matt Wilko

Just use the DateTime Constructorto create a new date value using 1 as the day of the month and set the datetime picker to that value:

只需使用DateTime 构造函数创建一个新的日期值,使用 1 作为月份中的第几天,并将日期时间选择器设置为该值:

dtpDate.Value = New DateTime(dtpDate.Value.Year, dtpDate.Value.Month, 1)

回答by Manoj Yadwad

I have found the answer very useful and very easy. I have provided the link below.

我发现答案非常有用且非常简单。我提供了下面的链接。

Getting first and last day of the current month

获取当月的第一天和最后一天

 DateTime now = DateTime.Now;
 var startDate = new DateTime(now.Year, now.Month, 1);
 var endDate = startDate.AddMonths(1).AddDays(-1);

Originally Posted by Marcin Juraszek (https://stackoverflow.com/users/1163867/marcinjuraszek)

最初由 Marcin Juraszek 发布 ( https://stackoverflow.com/users/1163867/marcinjuraszek)

回答by Nabeel Haxxan

You can change manually date, month and Year of the Selected DateTime in C# by using this I m changing the Selected Date only and set Selected Date 1st Of the selected moth year...

您可以在 C# 中手动更改所选日期时间的日期、月份和年份,方法是使用此我仅更改所选日期并设置所选日期的第 1 个所选月份...

 var moodifiedDate = new DateTime(input.Year, input.Month, 1)