How to get the month or day of the week in Portuguese on C#?

Asked

Viewed 1,259 times

6

In PHP, through the function strftime, I can return the name of the day of the week or month, in the language installed in the operating system.

Example:

echo strftime("%d %B", time());

Upshot:

16 January

I need to write the full date in C# in Portuguese and I would like to know if you can do the same in PHP.

2 answers

10


You can do it this way:

DateTime now = DateTime.Now;
Console.WriteLine(now.ToString(@"d  MMMM", new CultureInfo("PT-pt")));

Upshot

16 January

Example

First you take the current date

DateTime now = DateTime.Now;

Then convert it to string using the method ToString and format it using the first format @"d MMMM" and in the second parameter you set the language using the object CultureInfo.

  • Won’t that be based on the language of the system only? I did a test on ideone here and returned to me in English.

  • I’ll edit the answer, I really didn’t notice this detail

  • Edited showing in Portuguese, thanks @Articunol :)

  • 1

    Agora ta exibindo em português no ideone :)

1

You can do so, that you are free to control the date format and language.

        CultureInfo pt = new CultureInfo("pt-PT",false);
        string dateFormatString = pt.DateTimeFormat.LongDatePattern;

        string data =  DateTime.Now.ToString(dateFormatString,new CultureInfo("pt-PT"));

In this case

16 de January de 2019

  • But will this code there not display in English? The question says you want to display in English.

  • I tested your code in ideone and it presents the date in full English.

  • Yes, if you want in PT just change to en-PT

  • 2

    It would be interesting if you edit the answer with this then, since it is a requirement of the question to be in Portuguese.

  • That’s it. It was meant to see how it works.

  • Sergio, I made a test again in the ideone, and the full date continues to display in English in the ideone. I believe it is dependent on the system in which it is running, this may be a problem for the author.

  • My system is running all over EN.

  • See for yourself: https://ideone.com/qXj3GV

  • @Articunol You’re right, I did the test on https://dotnetfiddle.net/ and it’s on EN. It is sesnsible to the user’s idiom, in my case although the system is in EN my language is in PT

  • @Articunol so is in the expected language. Thank you.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.