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

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

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

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.

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*;*

  •  	if(item==' ')*
    
  •  	{*
    
  •  		switch (z)*
    
  •  		{*
    
  •  		case 0:*
    
  •  			computerDate=add;*
    
  •  			break;*
    
  •  		case 1:*
    
  •  			computerTime=add;*
    
  •  			break;*
    
  •  		}*
    
  •  		z++;*
    
  •  		add="";*
    
  •  	}*
    
  •  	else*
    
  •  	{*
    

_ add+=currentTime*;_
_
}_
_
}*_