Solved

what is a safe method of formatting dates to such as dd/mm/yyyy in C#

Posted on 2009-07-01
5
257 Views
Last Modified: 2013-12-17
I am relatively new to C#. been googling about this but see no good way of doing what I want.
i want to convert any type of date to dd/mm/yyyy ( or dd-mm-yyyy for that matter ).
The string I want to convert can be in ay valid date format such as
MM/dd/YYYY  
MM.dd.yyyy
dd/mm/yy

Thanks very much.
0
Comment
Question by:indikad
5 Comments
 
LVL 39

Accepted Solution

by:
abel earned 125 total points
ID: 24754233
Before you convert it, you need to know what format you use for input. Often, when you "just" use DateTime.Parse it will go correct (use .TryParse to find out whether it is parsable at all and prevent an error). You cannot reasonably expect to know beforehand that a date like 03/05/2006 is 03 May or 05 March. If you cannot know that, the computer cannot either.

Can you update with how we can determine what format to use from the three possibilities above? How do you define "any" date format, because there are literally thousands and thousands of date formats in the world....
0
 
LVL 2

Assisted Solution

by:softbee
softbee earned 125 total points
ID: 24754271
Date and Time representations are - in all MS language versions - coupled to the 'LOCALE' that the computer is in. In programs written for the international market it is a given that the date/time shown by a program will be that which the person using the machine is used to. Taking proper care with the 'LOCALE' makes this mostly fully automatic.
The keywords Locale and CultureInfo in MSDN should lead you in the right direction.
0
 
LVL 3

Assisted Solution

by:Dov_Landau
Dov_Landau earned 125 total points
ID: 24754417

If you use the CultureInfo then you can not say that every Date string is appropriate for the conversion, because only valid dates - according to the specific culture - are acceptable.
If you do not use the culture info - then you can not parse every valid string date, because you don't know how to distignuish between dates.
For example: if you get 11/12/2008. you don't know whether the 11 means month and 12 means day, or vice versa.
0
 
LVL 8

Assisted Solution

by:dericstone
dericstone earned 125 total points
ID: 24756471
Since you say you are relatively new to C#, you might not realize that most types have a ToString() method that will create a string representation of the object. So once you have parsed the input string into a DateTime object (as abel explained above) you can just call ToString() to get the newly formatted string. I think there are more options for formatting the string, but that should get you started.
0
 

Author Closing Comment

by:indikad
ID: 31598759
Thank you for all - now I have a good starting point and some very good directions.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now