Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Convert dd/MMMM/yyyy to yyyymmdd (Russian)

Tags:

c#

datetime

31 декабря 2016 в 15:10

декабря is not English.

декабря = December

DateTime.Parse("31/December/2016").ToString("yyyymmdd"); => 20161231

DateTime.Parse("31/декабря/2016").ToString("yyyymmdd"); => Error

31 декабря 2016 в 15:10 => 20161231

How do I convert it?

like image 679
canfas Avatar asked Feb 05 '23 13:02

canfas


1 Answers

The DateTime format above should be converted using TryParseExact with GetCultureInfo set to Russian culture like this:

String example = "31/декабря/2016"; // December 31, 2016

DateTime result;

bool check;
check = DateTime.TryParseExact(example, "dd/MMMM/yyyy", CultureInfo.GetCultureInfo("ru-RU"), DateTimeStyles.None, out result);

String converted = result.ToString("yyyyMMdd");

Console.WriteLine(check);
Console.WriteLine(converted);

The output returned by console is:

True
20161231

NB: To convert date with spaces instead of slashes between date components, change "dd/MMMM/yyyy" to "dd MMMM yyyy" (use another format to convert time part together).

Working example: .NET Fiddle Demo

like image 72
Tetsuya Yamamoto Avatar answered Feb 12 '23 12:02

Tetsuya Yamamoto