Posts Tagged ‘DateTime’
How to get the difference between two dates in .net
Posted by: admin in .NET, Date and Time on August 10th, 2010
You can use the built-in function for getting the difference between two dates.
Example:
-
Dim d1 As DateTime = Now
-
Dim d2 As DateTime = Now.AddDays(3)
-
-
Dim m As TimeSpan = d2.Subtract(d1)
-
Dim t As String
-
-
t = "Days : " & m.Days.ToString() & vbCrLf
-
t += "Hours : " & m.Hours.ToString() & vbCrLf
-
t += "Minutes : " & m.Minutes.ToString() & vbCrLf
-
t += "Seconds : " & m.Seconds.ToString() & vbCrLf
-
t += "Milliseconds : " & m.Milliseconds.ToString() & vbCrLf
-
MessageBox.Show(t)
How to calculate Time span in .Net
Here’s how to code the time span;
Function ExecuteLargeRec() {
DateTime d1 = DateTime.Now
DateTime d2
TimeSpan ts
‘ Your query here / processing
d2 = DateTime.Now
ts = d1.Subtract(d2)
MessageBox.Show(ts.ToString())
}
How to assign Null Values to Date Type Variable in .Net
Posted by: admin in .NET, Date Formatting, VB.Net on May 18th, 2009
Actually, you can’t assign a null value to a Date Type variable but you can check if the variable has been set or not by comparing its current value versus default value (#12:00:00 AM#) of date/datetime, you can get the default value by using
-
Date.MinValue or DateTime.MinValue
To do this, let say you have a variable called LastUpdate with a data type of Datetime
-
Dim LastUpdate as DateTime
-
dim dr as Datarow
-
-
If DateTime.MinValue = LastUpdate Then
-
'Its Null value
-
'if you want to save the value to the database then use the DBNull.Value
-
dr("LastUpdate") = DBNull.Value
-
Else
-
dr("LastUpdate") =LastUpdate
-
End If
How to Convert String to Date Format
Posted by: admin in .NET, C#, Date Formatting, String Manipulation, VB.Net on May 3rd, 2009
To Convert String To Date Format, use the DateTime.ParseExact function
DateTime.ParseExact(s as String, format as String, provider As IFormatProvider) as DateTime
Parameters:
- s
- Type: System.String
A string that contains a date and time to convert.
- format
- Type: System.String
A format specifier that defines the required format of s.
- provider
- Type: System.IFormatProvider
An object that supplies culture-specific format information about s.
Example:
-
Private Function ToDate() As DateTime
-
-
Dim str As String = "20060505"
-
-
Dim mDate As DateTime
-
-
-
-
mDate = DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat)
-
-
Return mDate
-
-
End Function
Read More about DateTime.ParseExact Method (String, String, IFormatProvider)