locked
Format for Date - Incorrect Format for Spanish RRS feed

  • Question

  • Hi guys,

    I was trying to cast some date into an specific format in Spanish. The initial string I'm using is as an example: "01/31/2019"

    So, what I expect from the casting is: "31-Ene-2019", but I'm receiving the following "31-ene.-2019" (month without initial Capital letter and a dot symbol).

    Here the sample code I created:

    public void ObtainDate()
    	{
    		DateTime dateConverted = DateTime.Parse("01/31/2019");
    		
    		Console.WriteLine(dateConverted.ToString("dd-MMM-yyyy", CultureInfo.CreateSpecificCulture("es-ES")));
    	}

    Please can anybody give me a hand on this?

    Thanks a lot,

    Wednesday, August 12, 2020 8:09 PM

All replies

  • See the following issue

    https://github.com/moment/moment/issues/1234


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, August 12, 2020 9:46 PM
  • Hi Kent-15,

    Thank you for posting here.

    Use es-US to remove the dot at the end of the month.

      Console.WriteLine(dateConverted.ToString("dd-MMM-yyyy", CultureInfo.CreateSpecificCulture("es-US")));

    A comment in this link says that there are no capital letters in Spanish months.

    But I don't know Spanish, so I can't confirm whether the information is correct and appropriate, so I think you'd better check it in the Spanish forum.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, August 13, 2020 2:11 AM
  • Here is an ugly solution. It assembles the final result from the parts of the date, after truncating and applying upper case to the month:

    using System.Globalization;
    …
    
    DateTime dateConverted = DateTime.Parse("31/1/2019"); // example
    
    var cultureInfo = new CultureInfo("es-ES");
    string text = dateConverted.Day +
        "-" +
        cultureInfo.TextInfo.ToTitleCase(cultureInfo.DateTimeFormat
            .GetMonthName(dateConverted.Month).Substring(0,3)) +
        "-" +
        dateConverted.Year;
    

    It is not pretty, but it does the job.

    Note that capitalizing the first letter of the month in Spanish is dependent on your age: those of us who are a bit older were taught when we were at school that month names should be capitalized. But a few decades later the rule was changed, and nowadays what they teach you is that they should not be capitalized.

    • Proposed as answer by Naomi N Thursday, August 13, 2020 12:49 PM
    Thursday, August 13, 2020 12:43 PM
  • Hi,

    Has your issue been resolved?

    If so, please click on the "Mark as answer" option of the reply that solved your question, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 21, 2020 6:53 AM