I have a DateTimePicker in a Form. I need to restrict the user to only select a Date from the current Month: for example, from 1 November 2018 to 30 November 2018.
I tried these lines of code:
DtpIssue.MinDate = New Date Time(DateTime.Now.Year, DateTime.Now.Month, 1)
DtpIssue.MaxDate = New Date Time(DateTime.Now.Year, DateTime.Now.Month, 1)
But it shows only the current date: 10 November 2018.
What should I do to fix the current Month for the DateTimePicker?
The last day of the month is returned by DateTime.DaysInMonth(), passing the current Year and Month.
Dim FirstOfMonth As Date = New DateTime(Date.Now.Year, Date.Now.Month, 1)
Dim EndOfMonth As Date =
New DateTime(Date.Now.Year, Date.Now.Month, Date.DaysInMonth(Date.Now.Year, Date.Now.Month))
In a similar format, applied to your controls:
DtpIssue.MinDate = New Date(Today.Year, Today.Month, 1)
DtpIssue.MaxDate = New Date(Today.Year, Today.Month, Date.DaysInMonth(Today.Year, Today.Month))
Related
Am working in windows application.i am scanning my date barcode
i have a date barcode ..while scanning that barcode i am getting value to my textbox like this :
17197...so this value means 2017 july 16,
in that 17-year
197 -calculation of days from 2017 to july 16..
while reading this barcode i want to just populate correct date to datetime picker
how i can convert this value to date time picker..
any help is very appriciable..thanks in advance
If the first two numbers represent the year after 2000 and the following numbers represent the days from the first of january (included) of such year then
Dim test = "17197"
Dim year = Convert.ToInt32(test.Substring(0,2)) + 2000
Dim days = Convert.ToInt32(test.Substring(2))
Dim currentDate = new DateTime(year, 1, 1).AddDays(days-1)
Console.WriteLine(currentDate) ' 16/07/2017
How to calculate the last day of the next month using vb.net?
try yo use DaysInMonth Function which returns the number of days in the required month.
Example
System.DateTime.DaysInMonth(2016,May)
This will return (31), so you can get the date format in addition to this number to get last day of this month.
An other way is to get the first day of the month after and substract 1 day.
Dim d As DateTime
d = DateTime.Now.AddMonths(2)
d = New DateTime(d.Year, d.Month, 1).AddDays(-1)
If you after the actual date....
You could use DateSerial(Year, Month, 0) this will return the last day of the month prior to the month entered.
Entering DateSerial(2016, 07, 0) will return "30/06/2016"
To get the last day of next month do DateSerial(Now.Year, Now.Month + 2, 0)
This works fine for February and also the year end as well (DateSerial(2016, 3, 0) returns "29/02/2016", DateSerial(2016, 11 + 2, 0) returns "31/12/2016")
Dim N = DateTime.Now.AddMonths(1)
Dim D = New DateTime(N.Year, N.Month, DateTime.DaysInMonth(N.Year, N.Month))
D will have the last day of next month.
Following code gives last day of the previous month.
MonthCalendar1.SelectionEnd = MonthCalendar1.SelectionStart.AddDays(-MonthCalendar1.SelectionStart.Day)
Which code gives first day of MonthCalendar1 selected year?
So if selected date is for example 13.06.2015 how to get 01.01.2015?
Just create a new Date using only the year of the reference date like This:
Dim firstOfYearDate As Date = New Date(baseDate.Year, 1, 1)
Like This
MonthCalendar1.MaxSelectionCount = 365
MonthCalendar1.SelectionEnd = New Date(2015, 06, 13)
MonthCalendar1.SelectionStart = New Date(MonthCalendar1.SelectionEnd.Year, 1, 1)
If you want to go with your current method of adding negative days, simply subtract one from DateTime.DayOfYear:
MonthCalendar1.SelectionEnd = MonthCalendar1.SelectionStart.AddDays(-(MonthCalendar1.SelectionStart.DayOfYear-1))
If you want the last day of the selected year, use DateTime.DaysInMonth():
Dim dt As New DateTime(MonthCalendar1.SelectionStart.Year, 12, DateTime.DaysInMonth(MonthCalendar1.SelectionStart.Year, 12))
I need help on getting the starting date of a week from a given week number in vb.net.
I have combobox binded with items 1-53 as week numbers. What I want to do is whenever I select a specific week number, it will return the first date of that selected week number.
For instance I have selected week Number 46, it must return November 11, 2013 as first date of week number 46.
I have tried the following codes and it returns only the first date of a week from a current week number.
DateAdd("d", -Weekday(Now, FirstDayOfWeek.Monday) + 1, Now)
Is there any possible way that can return my expected output?
Please help me. Thanks in advance.
Public Function FirstDateOfWeek(ByVal Year As Integer, ByVal Week As Integer, Optional FirstDayOfWeek As DayOfWeek = DayOfWeek.Monday) As Date
Dim dt As Date = New Date(Year, 1, 1)
If dt.DayOfWeek > 4 Then dt = dt.AddDays(7 - dt.DayOfWeek) Else dt = dt.AddDays(-dt.DayOfWeek)
dt = dt.AddDays(FirstDayOfWeek)
Return dt.AddDays(7 * (Week - 1))
End Function
You could try something like this:
Private Sub TestDateAdd()
Dim weekStart As DateTime = GetWeekStartDate(46, 2013)
Console.WriteLine(weekStart)
End Sub
Private Function GetWeekStartDate(weekNumber As Integer, year As Integer) As Date
Dim startDate As New DateTime(year, 1, 1)
Dim weekDate As DateTime = DateAdd(DateInterval.WeekOfYear, weekNumber - 1, startDate)
Return DateAdd(DateInterval.Day, (-weekDate.DayOfWeek) + 1, weekDate)
End Function
Disclaimer: I only really tested this with the one input, you probably want to make sure that it works as expected for different years, etc..
My user will typically enter some trip info including a day and month but typically they just enter the day. For example they would enter "TRIP1500/31" where the 31 is implied that its in JULY. The trip date can be at most 7 days in the past or 7 days in the future. So now what Im trying to do is guess what month that day is meant to be. So far I have:
Dim diff As Integer = CInt(tripDay) - Date.Now.Day
Select Case diff
Case 0
'same day so its probably current month
End Select
What I'm having trouble with is the other cases where the current day and the trip day overlap month-to-month. If the current day and trip day are in current month then the most difference they can be is +/-7 days but what about the other cases? Any help appreciated.
Function GetTripDate(day As Integer) As Date
Dim today As Date = Date.Today
For i As Integer = -7 To 7
Dim dt As Date = today.AddDays(i)
If dt.Day = day Then Return dt
Next
Throw New ArgumentOutOfRangeException("Invalid trip day.")
End Function
This gives you the date(incl. month) of the nearest date with the given day:
Dim maxDiffDays = 7
Dim tripDay = 31
Dim today = Date.Today
Dim tripDate = New Date(today.Year, today.Month, tripDay)
Dim tripDates = {tripDate.AddMonths(-1), tripDate, tripDate.AddMonths(1)}
Array.Sort(Of Date)(tripDates, Function(d1, d2) ((today - d1).Duration).CompareTo((today - d2).Duration))
Dim nearestDate = tripDates.First()
If ((today - nearestDate).Days <= maxDiffDays) Then
Console.WriteLine("Nearest month for trip date is: " & nearestDate.Month)
End If
It creates a Date from a given day, then it creates the two surrounding dates one month after and previous this date. This array will be sorted from the positive timespan from today(TimeSpan.Duration). So the firs't date in the array is the nearest date with the correct month.