473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Thread: Date and Time

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 21 '05 #1
4 1925
"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 21 '05 #2
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 21 '05 #3
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 21 '05 #4
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 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Stefan Richter | last post by:
Hi, after coding for days on stupid form validations - Like: strings (min / max length), numbers(min / max value), money(min / max value), postcodes(min / max value), telefon numbers, email...
0
by: fowlertrainer | last post by:
Hello ! I have been created a Zope (Python) Product. But it is must store inner global definitions (some conversion parameters), what I need to change in only one thread, or only once. See...
6
by: vee_kay | last post by:
Ihave a written aprogram in C which implements _beginthread(to create a thread) and _endthread(to end a thread).The program need to write a string of date n time to a file for each succesful thread...
7
by: David Sworder | last post by:
Hi, I'm developing an application that will support several thousand simultaneous connections on the server-side. I'm trying to maximize throughput. The client (WinForms) and server communicate...
20
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
4
by: Ben Fidge | last post by:
My application uses a singleton static class for writing entries to a log file. The location and name of the log-file is read from web.config each time an entry is written, but has the current date...
11
by: Audrey | last post by:
Please why when I write : while(1){ Console.writeln("date= {0:HH:mm:ss.ffff}", DateTime.Now); System.Threadind.Thread.Sleep(40); } I obtain date= 16:04:35.6250 date= 16:04:35.6718 date=...
9
by: esakal | last post by:
Hello, I'm programming an application based on CAB infrastructure in the client side (c# .net 2005) Since my application must be sequencally, i wrote all the code in the UI thread. my...
5
by: Tsair | last post by:
I set the thread culture in MAIN() as below in order to show the date in format DAY/MONTH/YEAR, but the datagridview alway show the date in M/d/yyyy. How to set the default Date format from...
7
by: Smokey Grindle | last post by:
I have my entire program in a Sub Main class and it runs as a notify tray icon 99% of the time... inside the program there is a timer whihc is declared like this Dim WithEvents MyTimer As New...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.