?
Solved

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

Posted on 2009-07-01
5
Medium Priority
?
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 39

Accepted Solution

by:
abel earned 500 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 500 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 500 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 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

801 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