By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,347 Members | 2,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,347 IT Pros & Developers. It's quick & easy.

Thread: Date and Time

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"jty202" <jt****@gmail.com> schrieb:
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>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 19 '05 #2

P: n/a
Hello jty202,

Look at the DateTime class... It even has methods that let you compare the
instance with another to determine which is greater (without tying you to
a specific format).

--
Matt Berther
http://www.mattberther.com
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

Nov 19 '05 #3

P: n/a
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.

Nov 19 '05 #4

P: n/a
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/de...matstrings.asp

For information on formatting in .NET in general see:
http://msdn.microsoft.com/library/de...ttingtypes.asp

Hope this helps
Jay
"jty202" <jt****@gmail.com> wrote in message
news:OG**************@TK2MSFTNGP12.phx.gbl...
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

Nov 19 '05 #5

P: n/a
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/de...exacttopic.asp

In VBNet there is as well CDate
http://msdn.microsoft.com/library/de...afctcdatex.asp

As it is recomended on MSDN to use in connection with VBNet (search for
Cdate)
http://msdn.microsoft.com/library/de...tinternals.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/de...etimetopic.asp

I hope this give some idea's?

Cor
Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.