Solved

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

Posted on 2009-07-01
5
258 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 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