Before updating to Windows 10 you may need to return your Pro license. More information here

Formatting Date and Time


i have a textfield taking a string representing a specific DateTime format. I want the string representing the DateTime to be formatted as YYYY-MM-DD, how can i do this?


more ▼

asked Jun 04, 2010 at 09:30 AM

avatar image

50 20 21 21

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Use DateTime.TryParse.

You'll probably run into issues if you try to overwrite the string the user is typing while they do that, but if you have an input field and a separate display field that shows the "parsed" date time (formatted like Duck's answer) that would work.

more ▼

answered Jun 18, 2010 at 04:09 PM

avatar image

8.7k 64 81 160

(comments are locked)
10|3000 characters needed characters left

Is your value coming from an actual DateTime object? If so, you can do this:

yourTextField.text = String.Format("{0:yyyy-MM-dd}", yourDateTimeObj);
more ▼

answered Jun 04, 2010 at 11:34 AM

avatar image

duck ♦♦
51.5k 147 418 546

no my value is comming from the text field which is a string

Jun 04, 2010 at 12:35 PM Ruben

ok in that case your question isn't very clear. You have it in a string already, and you want it formatted in a different way? What format is it to begin with?

Jun 04, 2010 at 03:07 PM duck ♦♦

@Duck, I suspect he means, that a user could enter some random text into a Textfield, and he wants to clean it up. But who knows?

Jun 04, 2010 at 04:22 PM Cyclops

@Cyclops thats what i mean. the user enters a number format for the date (as string) and i want to ensure that its formatted as YYYY-MM-DD.

Jun 06, 2010 at 02:48 PM Ruben
(comments are locked)
10|3000 characters needed characters left
 string computerDate;
 string computerTime;
 void computerDateAndTime() // read computer current time and date
         string currentTime=System.DateTime.Now.ToString();
         int z=0;
         string add="";
         for(int i=0;i<currentTime.Length;i++)
             char item = currentTime[i];
             if(item==' ')
                 switch (z)
                 case 0:
                 case 1:
more ▼

answered Jun 02, 2014 at 12:32 PM

avatar image

0 1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Jun 04, 2010 at 09:30 AM

Seen: 8006 times

Last Updated: Jun 02, 2014 at 12:32 PM