Asp.net Get/Set Date and Time as String

In many occasions all we may need is simple date and time.

Now property gives more information which needs to be trimmed down using Format function. Here we have two handy string type properties from Microsoft.

TimeString Property

Returns or sets a String value representing the current time of day according to your system.

TimeString always returns the system time as “HH:mm:ss“, which is a 24-hour format. This format is culture-invariant, which means it does not change even if you change the Regional Options in Control Panel.

ex :

[sourcecode language=’vb’]
Dim MyTime As String = TimeString

If CInt(TimeString.Substring(0, 2)) < 12 Then
MyGreeting = Reverse(“Good Morning”)
Else
MyGreeting = Reverse(“Good Afternoon”)
End If
[/sourcecode]

Timeofday : returns / sets the same but as date datatype.

ooOoo

DateString Property

DateString always returns the system date as “MM-dd-yyyy“, which uses the abbreviated month name.

These formats are culture-invariant, which means they do not change even if you change the Regional Options in Control Panel.

Today : returns / sets the same but as date datatype.

kick it on DotNetKicks.com

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: