3

I have a date string like "30/01/2018" and I want a format like "2018/01/30"

I have tried the following lines,

DateTime.Parse(startDate.Text.ToString()).ToString("yyyy/MM/dd");

and

DateTime.ParseExact(startDate.Text.ToString(),"dd/MM/yyyy").ToString("yyyy/MM/dd");

but didn't work. Anyother solution?

6
  • what you have in startDate.Text Commented Jan 30, 2018 at 7:36
  • Your code uses the default locale of your server or client machine where it executes. If the locale cannot understand the format of your strings it cannot convert to a proper datetime value. What is the locale where this code runs? (By the way, Text is already a string property, no sense in calling ToString on it) Commented Jan 30, 2018 at 7:38
  • are you passing it to a new variable? Commented Jan 30, 2018 at 7:39
  • Also there is no overload of ParseExact with only two arguments, the same for the first example. Both don't compile if you don't add a CultureInfo parameter Commented Jan 30, 2018 at 7:41
  • startDate.Text = "30/01/2018" @un-lucky Commented Jan 30, 2018 at 7:43

6 Answers 6

6

try like this

string newFormat = DateTime.ParseExact("30/01/2018", "dd/MM/yyyy", CultureInfo.InvariantCulture)
    .ToString("yyyy/MM/dd", CultureInfo.InvariantCulture);
Sign up to request clarification or add additional context in comments.

Comments

3

If you want output in string. Why not split and re-arrange?

string strOldFormat = "30/01/2018";
string[] strArrOldFormat = strOldFormat.Split('/');
string strNewFormat = strArrOldFormat[2] + "/" + strArrOldFormat[1] + "/" + strArrOldFormat[0];

Comments

3

Escape your slashes (and use a Culture with ParseExact)

DateTime.ParseExact("30/01/2018","dd/MM/yyyy", CultureInfo.InvariantCulture)
    .ToString(@"yyyy\/MM\/dd")

Or use the InvariantCulture for the ToString instead of escaping the slash:

...ToString(@"yyyy/MM/dd", CultureInfo.InvariantCulture)

Reason is, that / is a special format character for dates:

Custom Date and Time Format Strings - The "/" custom format specifier

The "/" custom format specifier represents the date separator, which is used to differentiate years, months, and days. The appropriate localized date separator is retrieved from the DateTimeFormatInfo.DateSeparator property of the current or specified culture.

6 Comments

it is the backslash character that needs to be escaped not the forward slash.
@Steve does the edit answer your concerns? And backslash doesn't need escape with @"string"
you don't need to escape it. Just use CultureInfo.InvariantCulture in ToString() method and you don't have to worry about culture's date separator.
@grek40 as you don't need the verbatim character before the first dd/MM/yyyy you don't need it and the backslash before the second
@Nino yes, I already mentioned this as a second option... but still it's equally valid to escape a special character in order to use it literally :)
|
2

Try using below sample code:-

var  actualStartDate = DateTime.ParseExact(startDate.Text.ToString(),"yyyy/MM/dd", CultureInfo.InvariantCulture);

Comments

1

you can try this code

string dateString = startDate.Text.ToString();
string format="yyyy/MM/dd"; 
string result;
CultureInfo provider = CultureInfo.InvariantCulture;
result = DateTimeOffset.ParseExact(dateString, format, provider).ToString();

or You can bundle it into one line like this

string result = DateTimeOffset.ParseExact(startDate.Text.ToString(),"yyyy/MM/dd", CultureInfo.InvariantCulture).ToString();

more information here

1 Comment

1

You can use this;

String.Format("{0: yyyy/MM/dd}", yourDate);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.