Archive for the ‘Date and Time’ Category

How to get the difference between two dates in .net

You can use the built-in function for getting the difference between two dates.

Example:

  1.         Dim d1 As DateTime = Now
  2.         Dim d2 As DateTime = Now.AddDays(3)
  3.  
  4.         Dim m As TimeSpan = d2.Subtract(d1)
  5.         Dim t As String
  6.  
  7.         t = "Days : " & m.Days.ToString() & vbCrLf
  8.         t += "Hours : " & m.Hours.ToString() & vbCrLf
  9.         t += "Minutes : " & m.Minutes.ToString() & vbCrLf
  10.         t += "Seconds : " & m.Seconds.ToString() & vbCrLf
  11.         t += "Milliseconds : " & m.Milliseconds.ToString() & vbCrLf
  12.         MessageBox.Show(t)

, ,

No Comments


How to get Month Name in .net


dim dt as Datetime

dt = Datetime.Today

Messabox.show(dt.ToString(“MMM”))

, ,

No Comments


How to get Julian Date format in .Net

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

See Julain Day
Julian Date in Excel

, ,

No Comments


Subtracting Days of the Date

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.

,

No Comments


How to assign Null Values to Date Type Variable in .Net

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

  1. Date.MinValue or DateTime.MinValue

To do this, let say you have a variable called LastUpdate with a data type of Datetime

  1. Dim LastUpdate  as DateTime
  2. dim dr as Datarow
  3.  
  4. If DateTime.MinValue = LastUpdate   Then
  5.         'Its Null value
  6.         'if you want to save the value to the database then use the DBNull.Value
  7.         dr("LastUpdate") = DBNull.Value
  8. Else
  9.         dr("LastUpdate") =LastUpdate  
  10. End If

, ,

No Comments


How to Convert String to Date Format

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:
 

  1.     Private Function ToDate() As DateTime
  2.  
  3.         Dim str As String = "20060505"
  4.  
  5.         Dim mDate As DateTime
  6.  
  7.  
  8.  
  9.         mDate = DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat)
  10.  
  11.         Return mDate
  12.  
  13.     End Function

Read More about DateTime.ParseExact Method (String, String, IFormatProvider)

,

No Comments



SetPageWidth