Jump to content
 cn.ConnectionString = @"Data Source=user3;Initial Catalog=office_db;Integrated Security=True";
            cmd.Connection = cn;
            cn.Open();
            string s = "select date from salary where Emp_ID ='3'";
            SqlCommand query = new SqlCommand(s, cn);
            SqlDataReader reader = query.ExecuteReader();
            reader.Read();
            String db = reader["date"].ToString();
            reader.Close();
            cn.Close();

 label2.Text= db;

so this is my code and im able to get the date and time both in my output when i display it in the label.  i want only my date to be there and only in this format: dd/mm/yyyy.

and then i want to get the date (dd) convert it to a int for doing some calculations.any ideas how this can be done?

Link to post
Share on other sites

Try this

https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx

 

example:

 

create a DateTime object using this.

DateTime date1 = DateTime.Parse(db);

Then you can get the date as Int32 using

date1.Day;

If you want to print the DateTime in dd/mm/yyyy format , do

label2.Text = date1.toString("dd/MM/yyyy");

 

Link to post
Share on other sites

If your column data type in the database is a datetime, you should be able to just do the following:

// Reads the column object and casts to DateTime object.
var myDateTime = (DateTime)dataReader["myDateColumnName"];

Then, to format the date time object on the screen, you'd simply do this:

// Sets label text to formatted date time.
myLabel.Text = myDateTime.ToString("dd/MM/yyyy");

If your column is a string (aka varchar), and you are storing dates in the database as a string (which I would recommend against) you can try something like parik mentioned for parsing your DateTime from the DB column.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×