Home > database >  DateTime.ToString("dd.MM.yyyy") giving wrong format
DateTime.ToString("dd.MM.yyyy") giving wrong format

Time:10-12

I'm trying to convert a Date, into a Romanian DateTime String. This is my code:

using System.Globalization;

var date = DateTime.Now;
var cult = CultureInfo.GetCultureInfo("ro-RO");

var dateToString = date.ToString(cult.DateTimeFormat.ShortDatePattern);


var result = ($"input: {date.ToString()} - formatted: {dateToString} - format:{cult.DateTimeFormat.ShortDatePattern}");

Console.WriteLine(result);

The date today is 11/10/2022 (October 11th). The problem is that I'm expecting the dateToString variable to have a value of "11.10.2022" but I'm getting a "10.11.2022" (in otherwords my dd.MM.yyyy format translates to a MM.dd.yyyy)

What can I do to get the correct string representation of my date?

Thanks a lot in advance!

CodePudding user response:

date.toString("MM.dd.YYYY");

You Can Convert the DateTime TO String("YYYY-MM-dd") // Here is Your DateTime Format Design

CodePudding user response:

on my side:

input: 11.10.2022 17:44:50 - formatted: 11.10.2022 - format:dd.MM.yyyy

if you say, formatted is not the same on your machine, that would be a major bug in your .net installation. Just out of curiosity: What does date.ToString("MM.dd.yyyy") yield?

  • Related