Thread: Date and Time

J

jty202

I have string that contains a date in this format (14-Jan-05). I want to
store in date object if theres one and access each part of the date (month,
year, dates, day of week).

Specifically I want to convert it in this format 20050114 because this way,
I can compare it with another date in this format to see which is greater.

Can something show me how to do that. Thanks
 
H

Herfried K. Wagner [MVP]

jty202 said:
I have string that contains a date in this format (14-Jan-05). I want to
store in date object if theres one and access each part of the date
(month,
year, dates, day of week).

Specifically I want to convert it in this format 20050114 because this
way,
I can compare it with another date in this format to see which is greater.

You can use 'Date.ParseExact' to parse the date string and then call the
'Date' object's 'ToString' method to convert it back to a string with a
certain format. Both, 'ParseExact' and 'ToString' will accept a date and
time format string:

Date and Time Format Strings
<URL:http://msdn.microsoft.com/library/en-us/cpguide/html/cpconDateTimeFormatStrings.asp>
 
N

Not Aaron

You could try:

Dim x As String = "14-Jan-05"
Dim f2 As String() = CDate(x).GetDateTimeFormats
MsgBox(f2(5).Replace("-", ""))

The GetDateTimeFormats is basically a string array that will contain
around 125 different ways to display a string. the 5th item in the
string array is "YYYY-MM-DD" format, and then you can just replace the
dashes with a blank character to get your desired format. Let me know
if that doesn't work for you.
 
J

Jay B. Harlow [MVP - Outlook]

jty202,
You can use DateTime.Parse or DateTime.ParseExact to parse a string into a
date. In VB.NET you can use CDate instead of DateTime.Parse.

DateTime.Parse (or CDate) is useful to convert a string to a DateTime based
on the current regional settings in Control Panel.

DateTime.ParseExact is useful to convert a string to a DateTime based on a
specific format or a specific region/culture.

Once you have a DateTime object, you can use the DateTime.Compare method to
compare them (instead of converting them to strings). In C# you can use the
overloaded comparison operators instead of DateTime.Compare.

Something like:

Dim s As String = "14-Jan-05"
Dim d1 As DateTime '= DateTime.Parse(s)
d1 = DateTime.ParseExact(s, "dd-MMM-yy", Nothing)
Dim d2 As DateTime = DateTime.Now
If DateTime.Compare(d1, d2) < 0 Then

End If

NOTE: You can use the DateTime.ToString to format a date in a specific
format

s = d1.ToString("yyyyMMdd")

For details on custom datetime formats see:

http://msdn.microsoft.com/library/d...s/cpguide/html/cpcondatetimeformatstrings.asp

For information on formatting in .NET in general see:
http://msdn.microsoft.com/library/d...y/en-us/cpguide/html/cpconformattingtypes.asp

Hope this helps
Jay
 
C

Cor Ligthert

jty

There are it seems endless methods to convert dates to strings and visa
versa.

You send this message crossposted to VB and CSharp. However there are as
well methods in the Microsoft.VisualBasic namespace so for that there is a
distinct between C# and VBNet when you don't reference that namespace in
C#.

Some people want to avoid the VisualBasic namespace in VBNet and than they
mostly use the parse functions as well when programming VBNet.

http://msdn.microsoft.com/library/d...l/frlrfsystemdatetimeclassparseexacttopic.asp

In VBNet there is as well CDate
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vafctcdatex.asp

As it is recomended on MSDN to use in connection with VBNet (search for
Cdate)
http://msdn.microsoft.com/library/d...tml/vbtchmicrosoftvisualbasicnetinternals.asp

Not at least there is as well the System.Convert.ToDateTime which works
directly in C# and VBNet which I found myself very nice, however I keep it
when possible in VBNet to the recomendation.
http://msdn.microsoft.com/library/d...ml/frlrfsystemconvertclasstodatetimetopic.asp

I hope this give some idea's?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top