473,799 Members | 3,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 19 '05 #1
5 1459
"jty202" <jt****@gmail.c om> 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.ParseExac t' 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/cpconDateTimeFo rmatStrings.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
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
You could try:

Dim x As String = "14-Jan-05"
Dim f2 As String() = CDate(x).GetDat eTimeFormats
MsgBox(f2(5).Re place("-", ""))

The GetDateTimeForm ats 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
jty202,
You can use DateTime.Parse or DateTime.ParseE xact 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.ParseE xact 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.Compar e method to
compare them (instead of converting them to strings). In C# you can use the
overloaded comparison operators instead of DateTime.Compar e.

Something like:

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

End If

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

s = d1.ToString("yy yyMMdd")

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.c om> wrote in message
news:OG******** ******@TK2MSFTN GP12.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
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.Visua lBasic 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
3926
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 adresses and so on. I thought it might be a better way to programm an automated, dynamic form validation that works for all kinds of fields, shows the necessary error messages and highlights the coresponding form fields.
0
1325
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 below. So: because the Zope is threaded application, the threads are use this product. Possible in same time.
6
8866
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 created. I had put a delay of a second so that the thread and io operation will occur after a second. Now i need to implement another thing which i need to make sure the run was actually a second. This is because if i add another delay of...
7
2873
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 via a socket connection (no remoting, no ASP.NET). The client sends a message to the server that contains some instructions and the server responds in an asynchronous fashion. In other words, the client doesn't block while waiting for the...
20
3036
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 delegate inside the UI that I call to update the progress meter. I use the Suspend() and Abort() methods based on button events. I can watch the progress meter increase just fine when the thread is running. When I select Start, I enable the Cancel...
4
1540
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 inserted into its name. For example, the string: c:\inetpub\wwwroot\myapp\log.txt will be changed to: c:\inetpub\wwwroot\myapp\log_20060216.txt
11
2315
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= 16:04:35.7187 date= 16:04:35.7656
9
3272
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 problem occurs when i try to show a progress bar. The screen freezes. I know i'm not the first one to ask about it. but i'm looking
5
17421
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 windows xp control panel by C# coding or is that any way to set the application date format to d/M/yyyy without change the windows xp regional setting ? Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false );
7
1712
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 System.Timers.Timer(20000) the timer checks every 20 seconds a website to pull information from... if a date is found on the page it looks at, it makes a list of dates and places them into a date collection declared like this
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10484
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10228
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9072
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.