-1

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!

Katia S.
  • 197
  • 2
  • 13
  • If `11/10/2022` turns into `10.11.2022` then clearly `11/10/2022` is being a date in [US-American notation](https://en.wikipedia.org/wiki/Date_and_time_notation_in_the_United_States) (i.e., month/day/year), and not day/month/year as you seem to believe. –  Oct 11 '22 at 15:22
  • `cult.DateTimeFormat.ShortDatePattern` [prints](https://dotnetfiddle.net/vsdEWf) `dd.MM.yyyy`. Why do you expect it to be something different? And no, with the code you've provided, `dateToString` is `11.10.2022`. – gunr2171 Oct 11 '22 at 15:25
  • The console output is: input: 11/10/2022 18:16:25 - formatted: 10.11.2022 - format:dd.MM.yyyy The formatted date is switched to a MM.dd.yyyy (instead of dd.MM.yyyy as I'm passing it) – Katia S. Oct 11 '22 at 15:27
  • No, incorrect. `11/10/2022 18:16:25` comes from `date.ToString()`. Where exactly are you passing "dd.MM.yyyy" to `date.ToString()`? Secret hint: You don't... ;-) –  Oct 11 '22 at 15:28
  • date.ToString(cult.DateTimeFormat.ShortDatePattern) = date.ToString("dd.MM.yyyy"); – Katia S. Oct 11 '22 at 15:29
  • I really don't understand why you're thinking this code is misbehaving. https://dotnetfiddle.net/YhvsAn It's doing exactly what you say it's doing. In `date.ToString()`, you don't give it a culture, so it does whatever your computer's culture is. `dateToString` is exactly how you explain it should be, because you give an explicit culture. – gunr2171 Oct 11 '22 at 15:30
  • Again: `date.ToString(cult.DateTimeFormat.ShortDatePattern)` is not outputting "11/10/2022 18:16:25". `date.ToString()` is. Why would you believe that doing `date.ToString(cult.DateTimeFormat.ShortDatePattern)` would have an effect on doing `date.ToString()`? Look closely at how you compose your result string... –  Oct 11 '22 at 15:30
  • The date is correct using the culture you picked (GetCultureInfo("ro-RO"). You probably are using the wrong culture. – jdweng Oct 11 '22 at 15:45

2 Answers2

0

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

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

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 15 '22 at 10:18
0

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?

Roku
  • 31
  • 2
  • Ι think it has to do with the "Regional Settings" set up on the Operating System. The problem is that I need my "ToString()" to work REGARDLESS of the Regional Settings. – Katia S. Oct 11 '22 at 15:52
  • @KatiaS, "_I need my "ToString()" to work REGARDLESS of the Regional Settings_" Then you need to change the current culture in your program (either for your entire app, or for the thread executing `DateTime.ToString()`), setting it explictly to the desired culture. –  Oct 11 '22 at 15:53
  • Do you have any idea how to do that? I'm googling it but not finding anything useful.... – Katia S. Oct 11 '22 at 16:01
  • https://learn.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo.currentculture?view=net-6.0 – Roku Oct 11 '22 at 16:06