Archive for the ‘Date and Time’ Category
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 get Month Name in .net
Posted by: admin in .NET, Date Formatting on May 25th, 2010
dim dt as Datetime
dt = Datetime.Today
Messabox.show(dt.ToString(“MMM”))
How to get Julian Date format in .Net
Posted by: admin in .NET, Date Formatting on June 8th, 2009
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MessageBox.Show(Strings.Format(Date.Today, “yy”) & Date.Today.DayOfYear.ToString.PadLeft(3, “0″))
End Sub
End Class
Subtracting Days of the Date
Posted by: admin in .NET, C#, Date Formatting, VB.Net on May 26th, 2009
To subtract the date with a specified number of days, use the built in shared function of the Date which Date.Today.Add(Days), where Days is a day number. Instead of passing a positive number of days, put a negative sign in it.
Example to subtract 2 days from the current date.
Date.Today.Add(-2)
or you can also use the DateAdd() function and pass a negative day number.
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)