How to get the Day I'm looking for with the DateTime tool in C #?

-2

I have a problem when trying to get a specific day with the DateTime property of C #, since it only brings me one number and it is always the 1st.

Date method code

var fecha = DateTime.Now.ToString("d/M/yyyy");
DateTime moment = new DateTime();
int dia = moment.Day;
MessageBox.Show(Convert.ToString(dia), "Error Title", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

and the result is as follows:

    
asked by David 17.01.2017 в 16:58
source

2 answers

1

If you want to see the date of the current date, you should do the following:

MessageBox.Show(DateTime.Now.Day.ToString(), "Error Title", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

Because what you are doing is initializing a default date that is 01/01/0001, so that will always show you 1

    
answered by 17.01.2017 / 17:05
source
0

This part of the code is unnecessary:

DateTime moment = new DateTime();
int dia = moment.Day;

simply use the date variable you created:

DateTime fecha = new DateTime();
fecha = DateTime.Now();
int dia = fecha.Day;
MessageBox.Show(Convert.ToString(dia), "Error Title", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    
answered by 17.01.2017 в 17:33