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 '10 at 09:30 AM

Ruben gravatar image

50 8 9 11

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

2 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 '10 at 04:09 PM

Tetrad gravatar image

7.8k 27 40 94

(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 '10 at 11:34 AM

duck gravatar image

duck ♦♦
45.7k 109 164 426

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

Jun 04 '10 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 '10 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 '10 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 '10 at 02:48 PM Ruben
(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 '10 at 09:30 AM

Seen: 4555 times

Last Updated: Jun 04 '10 at 09:30 AM