How to catch the current day

Asked

Viewed 866 times

0

How do I display the current day in a program? But I didn’t want to take the zero left. For example: the day 01 I want to be 1, the day 02 I want to be 2.

1 answer

5


The basis for what you need is the DateTime.Today (or DateTime.Now, the difference between them is that the Now contains the time).

This returns the current date, in an object of the type DateTime, to convert to string with specific formatting, you can use the method ToString.

The formatting you want is d/M/yyyy

DateTime.Today.ToString("d/M/yyyy");

If you want to return only the day, in string you will have to use the format "d " (note the space after "d"), because d itself represents a standard format.

However, keep in mind that the structure DateTime contains members as Day, Month, Year, among others. You probably need one of these.

See working on . NET Fiddle.

  • That way today for example would be 3/10/2018? If I want to catch only the day, I can put the "d" inside the Tostring?

  • @Guilhermewayne I made an edition in reply.

  • 1

    @Guilhermewayne You can also use Datetime.Now (https://www.dotnetperls.com/datetime-now)

  • Thank you very much LINQ helped a lot. Paulohdsouza helped me a lot tbm with this site

Browser other questions tagged

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