Home » excel » Is there any way to format date cell in Excel with Invariant culture like in C#

Is there any way to format date cell in Excel with Invariant culture like in C#

Posted by: admin April 23, 2020 Leave a comment

Questions:

The problem I am facing is that in a cell of an Excel sheet, a date is written. The cell is formatted with custom-format like:

[$-de]dd/mm/yyyy (dddd)

But only the (dddd) part is processed to give me “Sonntag” (Sunday in German language).

The datetime format is not fixed even though I have written the culture as “de” for the custom format. Is there any way to achieve the following result without altering the datetime format(yyyy-mm-dd)?

Tried                       Output                   Expected output
--------                  -----------------------   -------------------
[$-fr]yyyy-mm-dd (dddd) => 2019-07-31 (dimanche)    31/7/2019 (dimanche)  
[$-de]yyyy-mm-dd (dddd) => 2019-07-31 (Sonntag)     31.7.2019 (Sonntag)
[$-en]yyyy-mm-dd (dddd) => 2019-07-31 (Sunday)      7.31.2019 (Sunday)
How to&Answers:

Since you are specifying the exact data format in the string (the yyyy-mm-dd), excel will have to respect it when it opens. AFAIK, excel does not have a true “default” format that matches what you are looking for. If you open Excel and specify the format of a cell you can set the Culture/Country but you would still have to choose the format – the first one usually uses / as the separator.

But if you want to use what is in .NET, you could use CultureInfo.DateTimeFormat to get what I think you are after:

CultureInfo c;
var dt = new DateTime(2019,7,31);

c = new CultureInfo("fr-FR");
Console.WriteLine($"{c}: {c.DateTimeFormat.ShortDatePattern}");
Console.WriteLine(dt.ToString($"{c.DateTimeFormat.ShortDatePattern} (dddd)", c.DateTimeFormat));
Console.WriteLine();

c = new CultureInfo("de-DE");
Console.WriteLine($"{c}: {c.DateTimeFormat.ShortDatePattern}");
Console.WriteLine(dt.ToString($"{c.DateTimeFormat.ShortDatePattern} (dddd)", c.DateTimeFormat));
Console.WriteLine();

c = new CultureInfo("en-EN");
Console.WriteLine($"{c}: {c.DateTimeFormat.ShortDatePattern}");
Console.WriteLine(dt.ToString($"{c.DateTimeFormat.ShortDatePattern} (dddd)", c.DateTimeFormat));
Console.WriteLine();

Will give you this in the output:

fr-FR: dd/MM/yyyy
31/07/2019 (mercredi)

de-DE: dd.MM.yyyy
31.07.2019 (Mittwoch)

en-EN: M/d/yyyy
7/31/2019 (Wednesday)

To use in excel do this:

[TestMethod]
public void Culture_Info_Data_Format_Test()
{
    //https://stackoverflow.com/questions/56985491/is-there-any-way-to-format-date-cell-in-excel-with-invariant-culture-like-in-c-s
    var fileInfo = new FileInfo(@"c:\temp\Culture_Info_Data_Format_Test.xlsx");
    if (fileInfo.Exists)
        fileInfo.Delete();

    using (var pck = new ExcelPackage(fileInfo))
    {
        var dt = new DateTime(2019, 7, 31);
        var workbook = pck.Workbook;
        var worksheet = workbook.Worksheets.Add("Sheet1");
        worksheet.Column(1).Width = 50;

        var c = new CultureInfo("fr-FR");
        var format = $"[$-fr]{c.DateTimeFormat.ShortDatePattern} (dddd)";
        worksheet.Cells[1, 1].Value = dt;
        worksheet.Cells[1, 1].Style.Numberformat.Format = format;

        c = new CultureInfo("de-DE");
        format = $"[$-de]{c.DateTimeFormat.ShortDatePattern} (dddd)";
        worksheet.Cells[2, 1].Value = dt;
        worksheet.Cells[2, 1].Style.Numberformat.Format = format;

        c = new CultureInfo("en-EN");
        format = $"[$-en]{c.DateTimeFormat.ShortDatePattern} (dddd)";
        worksheet.Cells[3, 1].Value = dt;
        worksheet.Cells[3, 1].Style.Numberformat.Format = format;

        pck.Save();

    }

}

which gives this:

enter image description here

Answer:

Try to use below formula in another column/cell, assuming you have a date starting from A1

=TEXT(A1,"mm/dd/yyy (dddd[$-fr])")

Hope it helps!

You can change the text string type from “/” to “.” or whatever you want and the language format by changing this element [$-fr]