populate date time picker from number of days - vb.net

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

Related

DateTimePicker only for current Month

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))

VB.net work out number of days between two dates [duplicate]

This question already has answers here:
How to find the number of days between two dates / DateTimePickers
(3 answers)
Closed 6 years ago.
I am trying to work out the number of days between two dates and whether it is a full month or not.
I currently have this code:
Dim fromdate As Date
Dim todate As Date
Dim timespan
Dim num_days As String
Dim month As DateTime
fromdate = Date.Parse("01/01/2017")
todate = Date.Parse("31/01/2017")
TimeSpan = todate - fromdate
num_days = TimeSpan.Days
MsgBox(num_days)
And then I try to work out if it is a full month:
month = todate
If (month.Month = "02" And num_days = "28") Or num_days = "29" Or num_days = "30" Or num_days = "31" Or num_days = "0" Then
'do code here
Else
'do code here
End If
But this is proving to not work in February because it only sees the num_days as 27
Is there a way I can get the correct number of days between the two dates including the dates themselves being full days?
And how can I check if it is a full, correct month or not
UPDATE
The purpose of this is for a billing system so files are read into a database with from and to dates then it needs to work out the pricing from those dates.
So a product has a specific price, but first of all we need to work out whether to bill for part of a month or a full month (basically part of the product price or the full price)
So these 'Full Month' date range examples will bill the full price
Full Month:
01/01/2017 - 31/01/2017
25/01/2017 - 25/01/2017
18/01/2017 - 18/02/2017
10/01/2017 - 10/01/2018
Whereas, this date range for 'Part Month' will only bill for the number of days between the from and to date (+1 day)
Part Month
15/01/2017 - 31/01/2017
This would get what you specified:
Dim IsOneFullMonth = (d1.Day = 1 And d2 = d1.AddMonths(1).AddDays(-1))
Dim IsOnMonthLater = (d2 = d1.AddMonths(1))
The two states are not really the same thing in my understanding.
The second check is one month and one day.
(The first would match 'Jan 01 - Jan 31', the second one 'Jan 01 - Feb 01'.)
Also consider that neither check would match 'end of months' like 2016-02-29 - 2016-03-31 - you have to really define what you want to achieve in those cases.

What exactly happens if I add two dates in VBA

Please help me what happend here- I know it is stupid but I want to know.
The output of the below code is April-18-2105. How did it pop up?
Private Sub CommandButton1_Click()
Dim firstDate As Date, secondDate As Date
firstDate = DateValue("Jun 19, 2010")
secondDate = DateValue("oct 29,1994")
MsgBox (firstDate + secondDate)
End Sub
Thanks in advance!
Dates are actually stored as numbers, so if we look at the numeric value for these dates:
CLng(datevalue("Jun 19, 2010")) '// 40348
CLng(datevalue("oct 29, 1994")) '// 34636
and add them together:
40348 + 34636 = 74984
and convert that number back to a date:
CDate(74984) '// 18/04/2105
Because it's 74,984 days after 00/01/1900
dates are numbers, formatted as dd/mm/yyyy, I think its the days from 31/12/1899, so that's what's happening. The result is 40348+34636=74984, which is 18/04/2105.
Excel stores the date as the number days after a specified date. As such the integer representing the date of 2010/06/19 is 40348 and the integer representing 1994/10/29 is 34636. When you add those integers together you get 74984 which is the integer that represents 2105/04/18.
The reference date does vary from mac to windows. I am running on windows and I did not change my reference date settings, That is where those integers came from. Mac uses January 2nd 1904 as day 1 and Windows uses January 1 1900 as day 1.

Generate 29 days of February in .NET

I am trying to populate a dropdown with No.of days generated by the system by the following code
Sub LoadDay()
Try
ddlDay.Items.Clear()
For i As Integer = 1 To System.DateTime.DaysInMonth(DateTime.Today.Year, ddlMonth.SelectedValue)
ddlDay.Items.Add(New ListItem(i.ToString(), i.ToString("00")))
Next
ddlDay.Items.Insert(0, New ListItem("--Select--", -999))
Catch ex As Exception
End Try
End Sub
Problem is, for the month of February it shows only 28 days because the following code generates Days on the basis of current year and this year isn't a leap year
System.DateTime.DaysInMonth(DateTime.Today.Year, ddlMonth.SelectedValue)
How can I fix it to always show 29 days for the month of Februaray?
You can use DayInMonth in DateTime
Dim dayinMonth As String
dayinMonth = System.DateTime.DaysInMonth(2012, 2)
This allow you to know the day in month for 2015 February. Parameter inside stand for year and month
I'd be interested in knowing what your requirment is that you have to be able to select invalid days, but, if you don't want your days to change based on the year, just create your own array of days. This leaves your intent a bit more clear than putting an arbitrary year in the DaysInMonth function.
Dim Days = {31,28,31,30,31,30,31,31,30,31,30,31}
ddlDay.Items.Clear()
For i As Integer = 1 To Days(ddlMonth.SelectedValue-1)
ddlDay.Items.Add(New ListItem(i.ToString(), i.ToString("00")))
Next
ddlDay.Items.Insert(0, New ListItem("--Select--", -999))

VBA Set a specific date to be the first week of the year

I want to set a specific date (October 06 , 2014) to be the 1st Week of the year. So when I use the =weeknum() formulaR1C1 it will return the week number.
I'm currently working on a worksheet that gets updated daily so the week number will be updated every week only. In column ("D") indicates the week number. and column ("B") indicates the daily date.
If i use the =weeknum() formula on it returns the value of 41, but it needs to be week number 1.
How about creating your personalized function?
It just consists of counting how many days there are between the date you insert (myDate) and the date which is considered to be the first day of the year (first_day), dividing this number by 7, taking its integer and adding up 1. It will always give you the right one.
Public Function special_weeknum(ByVal myDate As Date) As Integer
Dim first_day As Date: first_day = #10/6/2014#
Dim myWeek As Integer: myWeek = Int((myDate - first_day) / 7) + 1
special_weeknum = myWeek
End Function
Please note that this approach would allow you also to pass the first day as a user input:
Public Function special_weeknum(ByVal myDate As Date, ByVal first_day As Date)
Dim myWeek As Integer: myWeek = Int((myDate - first_day) / 7) + 1
special_weeknum = myWeek
End Function
so that you will always be able to decide which is the day you must consider as first day of the year.