x


Formatting Date and Time

hi

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?

thanks

more ▼

asked Jun 04, 2010 at 09:30 AM

Ruben gravatar image

Ruben
50 20 17 19

(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

Tetrad gravatar image

Tetrad
8k 62 62 118

(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

duck gravatar image

duck ♦♦
46.8k 128 188 453

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:
                    computerDate=add;
                    break;
                case 1:
                    computerTime=add;
                    break;
                }
                z++;
                add="";
            }
            else
            {
                add+=currentTime[i];
            }
        }
more ▼

answered Jun 02 at 12:32 PM

FarazKhalid gravatar image

FarazKhalid
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

By RSS:

Answers

Answers and Comments

Topics:

x641
x79
x29

asked: Jun 04, 2010 at 09:30 AM

Seen: 5397 times

Last Updated: Jun 02 at 12:32 PM