473,383 Members | 1,862 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,383 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 16 '05 #1
4 5544
"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 16 '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 16 '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 16 '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 16 '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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...

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.