I can't use datetime subtract method - vb.net

I can't use following Datetime subtract method.
https://msdn.microsoft.com/ja-jp/library/system.datetime.subtract(v=vs.110).aspx
I use Microsoft Visual Studio Proffesional 2013 and VisualBasic2013 and .NetFrameWork Version 4.5.50938
I want to use above method. I can use Add method like this:
Dim dt As Datetime = Nothing
dt.Add(New TimeSpan(1))
I want to know why although there is the homepage that is Datetime.
Subtract, I can't use subtract method......

Let's see what happens when you try to create a DateTime as Nothing:
Dim dt As DateTime = Nothing
Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss.fffffff"))
we get "0001-01-01 00:00:00.0000000". This is the earliest datetime that can be represented by a DateTime. It can't hold a "value" of Nothing because it is a Value type: Why is null not allowed for DateTime in C#?.
Let's try adding 1 tick:
dt.Add(New TimeSpan(1))
Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss.fffffff"))
Oh! We get "0001-01-01 00:00:00.0000000" again. The tick was not added on. That is because the Add method returns a new DateTime - it does not affect the one that Add was invoked on.
Let's try again:
dt = dt.Add(New TimeSpan(1))
Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss.fffffff"))
Now we get "0001-01-01 00:00:00.0000001", as intended.
What happens if we try to go earlier than the first allowed DateTime?
Dim dt As DateTime = Nothing
dt = dt.Subtract(New TimeSpan(1))
It throws a System.ArgumentOutOfRangeException.

Related

What is the date format to convert from string to date

I have below code to get StringToDate as 04/29/2019, but when use CDate function it's giving me #4/29/2019 12:00:00 AM# value. which date format i have to use to get 04/29/2019
Dim StringToDate As Date
Dim strNewDate As String = "04/29/2019"
StringToDate = CDate(strNewDate)
You could use DateTime.TryParse
Dim MyDate As DateTime = Nothing
Dim strNewDate As String = "04/29/2019"
DateTime.TryParse(strNewDate, MyDate)
Edit (misread your question)-
A DateTime holds value and not String. To get what you asking after parsing the date you would need to convert it back to a string with a format method or use the following:
MyDate.ToShortDateString
The way a date is represented is by what the region settings of your OS have setup.
For example, if you were to open the region settings in Control Panel you would find a format for Short Date which by what you're describing is probably M/d/yyyy. Inside the dropdown are other formats you can set and if you were to set it to MM/dd/yyyy you would see that your same code would now display 04/29/2019. But Don't Do This as it only affects your machine. Instead, understand that a date is only number, and you can control how it is displayed, the same way Windows just did from my example. A perfect example of this is that you could set a DateTime variable in many different ways. Here's a few:
Dim dt1 As DateTime = #2019-12-14#
Dim dt2 As DateTime = #1/08/2019#
Dim dt3 As DateTime = #03/03/2019#
Dim dt4 As DateTime = #4/4/2019#
The results would be
12/14/2019 12:00:00 AM
1/8/2019 12:00:00 AM
3/3/2019 12:00:00 AM
4/4/2019 12:00:00 AM
because that is how windows is set up to show me a DateTime as a string.
When you want to display this date to the user in a different format you can simply supply a format to the .ToString() method or use one of the DateTime string conversion methods (ie, .ToShortDateString, .ToLongDateString, etc) You can show the date to the user in a more preferable format.
Taking the last example (dt4) you could use
Console.WriteLine(dt4.ToShortDateString) 'and show 4/4/2019
Console.WriteLine(dt4.ToLongDateString) 'and show Thursday, April 4, 2019
Console.WriteLine(dt4.ToString("MM/dd/yyyy")) 'and show 04/04/2019

How do i add 1 month to a date that already has been created on VB?

Hi I'm new to coding and am looking for some help! I've been trying to add 1 month to a date that I've already stored in my database and displayed on a gridview.
Dim dueDate As DateTime = lblDateA.Text.AddMonth(1)
I know the way that I've done it is wrong but i hope you get the idea i'm going for! Thanks in advance!
First convert the date to a DateTime and add Month to it.
Dim dueDate As DateTime = Convert.ToDateTime(lblDateA.Text).AddMonths(1)
Firstly, the Text of a Label is a String, so you can't add a month to it. Whatever you're displaying in that Label should already be being stored in a DateTime variable somewhere. You would then add one month to that and then display the new value, e.g.
Me.dateA = Me.dateA.AddMonths(1)
lblDateA.Text = Me.dateA.ToShortDateString()
You convert the text to a Date and then add the month.
Private Function ConvertDGVDateAndAddMonth(dateAsString As String) As Date
Dim dt As Date
If Date.TryParse(dateAsString, dt) Then
'parsed correctly so we can use the dt variable as a date
Return dt.AddMonth(1)
End If
'if it does not convert return Nothing
Return Nothing
End Function
USage:
Dim dt As Date = ConvertDGVDate(dgv.CurrentCell.Value.ToString)
'if this does not convert dt = Nothing
'be sure to check

DateAdd function to add months

I used this code to add months
dtExpiry = DateAdd(DateInterval.Month, intDuration, dtStartDate)
and I also tried this code,
dtExpiry = DateAdd("m", bytDuration, dtMemStartDate)
but every time it just adds Days not months. The date format has to be dd/mm/yyyy. I've changed my PC date format to dd/mm/yyyy but still it keeps adding Days instead of Months.
BTW I'm receiving the duration and startDate from the Main calling program which extract these values from a data file thats in CSV format.
Your code looks ok, but you may have an incorrect data type.
Try this and see if this works:
dtExpiry = DateAdd(DateInterval.Month, intDuration, CDate(dtStartDate))
If that works then check the data type of dtStartDate.
All this can be avoided if you switch Option Strict On, as the code won't compile if dtStartDate is not of type DateTime
Note that you should also be able to do this:
dtexpiry = dtStartDate.AddMonths(intDuration)
This will fail to compile even with Option Strict Off if dtStartDate is not a DateTime so could be a safer option.
Use datetime.tryparse to convert your string to a date, then use as below?
You can also specify a conversion and specifically tell it what is where in the string (m/d/y)
I.e.:
Dim mydate As New Date
mydate = DateTime.ParseExact(datestring, "dd/MM/yyyy", Nothing, DateTimeStyles.NoCurrentDateDefault)
Dim secondDate As New Date
secondDate = mydate.AddMonths(6)
Or am I missing your question?

DateAdd Function call

In my Windows Form I have a textbox for number of days to add and a textbox that has a date in it... On the button click event I need to use the "DateAdd" function to add the number of days in textbox number of days to the date entered in textxbox date? Can someone give me the correct syntax I have tried this and getting a syntax error.
DateAdd(DateInterval.Day, txtNumDaysTAdd.Text, txtDate.Text)
I than need to display the answer in a messagebox in the Longdate format using the formatdatetime function.
I would rather use .NET's DateTime functions to do this:
Dim myDate As DateTime = DateTime.Parse(txtDate.Text)
Dim daysToAdd As Integer = Integer.Parse(txtNumDaysTAdd.Text)
Dim result As DateTime = myDate.AddDays(daysToAdd)
Don't use DateAdd, it's only there for backwards compatibility with VB6. Use the methods on the DateTime type instead. In VB.NET, the keyword Date is a shortcut for the DateTime type, just as Integer is a shortcut for the Int32 type.
Dim originDate As Date = Date.Parse(txtDate.Text)
Dim daysToAdd As Integer = Integer.Parse(txtDaysToAdd.Text)
Dim result As Date = originDate.AddDays(daysToAdd)
MessageBox.Show(result.ToLongDateString())

How to add both date and time in DateTime variable

I have two datetime variables in VB.Net
Dim inDt As Datetime //contains date
Dim inTime As DateTime //contains time
I want to add both these variable in single DateTime variable , i didn't find any .Net function to do that.Is there any other way to do it?
Illustrating my comment above, you can do this:
DateTime combinedDateTime = inDt +
new TimeSpan(0, inTime.Hour, inTime.Minute, inTime.Second, inTime.Millisecond);
You could make use of the Ticks property and the AddTicks method:
Dim result As DateTime
result = inDt.AddTicks(inTime.Ticks)