Comparing


Is it in the last hour

if dtDateTime > System.DateTime.Now.AddHours(-1) Then 
{
}

if dtDateTime.AddHours(1).CompareTo(System.DateTime.Now) < 0  //more than 1 hour ago 
if dtDateTime.AddHours(1).CompareTo(System.DateTime.Now) < 0 //less than 1 hour ago


Is it in the last 24 hours

if dtDateTime > System.DateTime.Now.AddHours(-24) 
{
}

This will not work if convert a datetime to a string and then convert it back because the millisecond component will be lost

System.DateTime dtDateTime1; 
System.DateTime dtDateTime2;
if ( System.DateTime.Compare(dtDateTime1, dtDateTime2) = 0 )
{
'then they are equal
}

if ( System.DateTime.Compare(dtDateTime1, dtDateTime2) < 0 )
{
'then dtDateTime1 is earlier than dtDateTime2
}

if ( System.DateTime.Compare(dtDateTime1, dtDateTime2) > 0 )
{
'then dtDateTime1 is later than dtDateTime2
}


Dim oReturnDate1 As DateTime 
Dim oReturnDate2 As DateTime
If Not DateTime.TryParseExact("20150721 12:06:37",
                     "yyyyMMdd H:mm:ss",
                     Globalization.CultureInfo.InvariantCulture,
                     Globalization.DateTimeStyles.None,
                     oReturnDate1) Then
End If
oReturnDate2 = TimeZoneInfo.ConvertTimeBySystemTimeZoneId("20150721 12:06:37", "Eastern Standard Time")




© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext