I have 2 fields fromDate and toDate for a school's term. Say their values are '01-06-2013' to '01-09-2013'. How do I get the total number of days in the term, in vb.net?
You can try this in vb.net:
Dim t1 As DateTime = Convert.ToDateTime("01-06-2013")
Dim t2 As DateTime = Convert.ToDateTime("01-09-2013")
MessageBox.Show(t2.Subtract(t1).Days)
I think сlass DateDiff can help you: http://msdn.microsoft.com/ru-ru/library/ms189794.aspx
Try this (C#):
TimeSpan timespan = Convert.ToDateTime("01-09-2013").Subtract(Convert.ToDateTime("01-06-2013"));
Response.Write(timespan.Days+1);
Usually the Subtract method return a value that is one less than the correct value so added one to get excat no of days
OR:
DateDiff(DateInterval.Day , curDate, srDate) //can replace the first argument with "d"
Also check this link
Related
I need help with a quick question I a string "07/10/2014" how can I get first the year "2014",second the month "10" ,third the day- "07" with out "/" only the values in VB.NET
Please show me the full way how to do it.
First declare it like this Dim x as Date = "07/10/2014". And to get the individual values use x.Day, x.Month and x.Year
Use the DateTime.Parse() method then use the return DateTime structure to extract the Month, Day, Year properties (similar to DJK's answer above).
If the thread's current culture is set to one that understands "mm/dd/yyyy" format, then the code can be as simple as:
Dim dt As DateTime = DateTime.Parse("07/15/2014")
MessageBox.Show(String.Format("Month: {0}; Day: {1}; Year: {2}", dt.Month, dt.Day, dt.Year))
Have a look at this.
Dim MyDate As Date
MyDate = "07/10/2014"
MsgBox(Format(MyDate, "dd")) ' dd gives you day number
MsgBox(Format(MyDate, "MM")) ' MM gives you month number
MsgBox(Format(MyDate, "YYYY")) ' YYYY gives you year number
The full list of date fomatting string could be found here (MSDN)
UPDATE
Use following example to assign to a string variable
Dim DayOfString As String DayOfString
DayOfString = Format(MyDate, "dd")
I am retrieving data from DB where there is a separate date and time fields. I want to join them into a DateTime field in my VB.NET project.
How would you suggest accomplishing this?
I tried this but it's not working for me.
"String was not recognized as a valid DateTime."
Dim InDate As New DateTime(incident.IncidentDate.Value.Year, incident.IncidentDate.Value.Month, incident.IncidentDate.Value.Day, 0, 0, 0)
Dim InTime As New DateTime(1900, 1, 1, incident.IncidentTime.Value.Hour, incident.IncidentTime.Value.Minute, incident.IncidentTime.Value.Second)
Dim combinedDateTime As DateTime = DateTime.Parse((InDate.ToString("dd/MM/yyyy") + " " + InTime.ToString("hh:mm tt")))
rdtpIncidentDateTime.SelectedDate = combinedDateTime
You can just create a new DateTime value from the components in InDate and InTime:
Dim combinedDateTime As DateTime = New DateTime( _
InDate.Year, InDate.Month, InDate.Day, _
InTime.Hour, InTime.Minute, InTime.Second _
)
DateTime exposes a method called DateTime.TimeOfDay
You can simply use DateTime.Add to append the time to the date.
Dim dateAndTime As DateTime = myDate.Add(myTime.TimeOfDay)
Dim CombinedDate As Date = Date1.Date + Time1.TimeOfDay
This will work when you want to combine the DATE from one variable and the TIME from a different variable.
Date is an alias to DateTime. In VB.NET, they are the same thing. You can see all the aliases in this chart on MSDN. Link
You can do it in one line...declare your indate and use the incident values for the time instead of zeros.
I have a gridview that has three bound fields. "Date", which is the day a support ticket was added; "Date Fixed", which is the day a support ticket was resolved; and "Days Waited", which should be the number of days a ticket has gone unresolved.
Each row has a different date and could or could not have a dateFixed. I can use the RowDataBound event to calculate daysWaited from date and dateFixed, but I do not know how to then set daysWaited to the calculated value for each row.
I am using VB.NET and the .NET 4.0 framework.
Not sure how you are loading data, hopefully setting the datasource of the gridview. After you set the datasource, you can call this method. It will go through and compare dates on each row, it also checks if anything is there. This worked good during my testing. Hopefully you find this good for your use.
Private Sub CalcDays()
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim stDate As Date
Dim endDate As Date
Dim daysPassed As Integer = -1
If Not (GridView1.Rows(i).Cells(1).Text = " ") Then
stDate = DateTime.Parse(GridView1.Rows(i).Cells(0).Text)
endDate = DateTime.Parse(GridView1.Rows(i).Cells(1).Text)
While (stDate <= endDate)
stDate = stDate.AddDays(1)
daysPassed += 1
End While
GridView1.Rows(i).Cells(2).Text = daysPassed.ToString
Else
GridView1.Rows(i).Cells(2).Text = "NA"
End If
Next
End Sub
Here's a screenshot of my test...
I am pulling a date value from my database:
Dim qfresho = From p In dbConfig.Configs _
Where p.Description = "FROD" _
Select p.dateValue
Dim qfreshc = From p In dbConfig.Configs _
Where p.Description = "FRCD" _
Select p.dateValue
Then comparing these date values to the current date:
If qfresho.First.Value >= Date.Now And qfreshc.First.Value <= Date.Now Then
lblFreshman.Text = "Freshmen are currently eligible to register for a room."
Else
Dim frdays As TimeSpan
frdays = (qfresho.First.Value).Subtract(Now)
lblFreshman.Text = "Registration will open for freshmen in " & frdays.Days & " days."
End If
But for some reason its always returning the Else condition - even though the values in the database should make the query true. Any ideas? I'm guessing for some reason its not pulling the results as a date?
Date.Now returns a DateTime, so does your database have dates and times or just dates? If you just want the date, you can use Date.Today.
Do the dates have time components that could be throwing off the compares?
Why not just add two debugs before the comparison???
Debug.WriteLine(qfresho.First.Value.ToString("MM/dd/yyyy hh:mm:ss.ffff"))
Debug.WriteLine(qfreshc.First.Value.ToString("MM/dd/yyyy hh:mm:ss.ffff"))
That should help.
How do I subtract a month from a date object in VB.NET?
I have tried:
Today.AddMonths(-1)
However, given that Today is 01-Jan-2010, the result I get is 01-Dec-2010. The answer I want is 01-Dec-2009.
Is there a convenient way of doing this within the .NET framework?
You actually have to transport Today into a variable and let that assignment work there. The following code will produce the result you expect (I just verified it because your post made me think twice).
Dim dt As DateTime = Date.Today
dt = dt.AddMonths(-2)
Dim x As String = dt.ToString()
This works fine, you need to remember that the DateTime is imutable.
Dim d As DateTime
d = New DateTime(2010, 1, 1)
d = d.AddMonths(-1)
Have a look at DateTime Structure
A calculation on an instance of
DateTime, such as Add or Subtract,
does not modify the value of the
instance. Instead, the calculation
returns a new instance of DateTime
whose value is the result of the
calculation.
Dim d As DateTime = #1/1/2010#
d = d.AddMonths(-1)
I have used the following and it works.
Dim dtToday As DateTime = Date.Today
dtToday = dtToday.AddMonths(-2)