user3459997
user3459997

Reputation: 27

change day part of datetime variable

i am having one date field in my table and i am taking this date field value in my datetime variable.

eg: datetime dt=24/4/2014 12:00 am

now i want to set only day part of this datetime variable means i want to change from day 24 to day 1 like this:

dt=1/4/2014 12:00 am

how to do this in aspnet with c#?

Upvotes: 1

Views: 405

Answers (1)

Habib
Habib

Reputation: 223247

You can create a new DateTime object using DateTime Constructor (Int32, Int32, Int32, Int32, Int32, Int32). In that constructor call use year,month, hour, minutes, seconds from original dt and explicitly pass 1 for the day part. Like:

DateTime dt = GetDateTimeFromDB(); //returns 24/4/2014 12:00 am
DateTime modifiedDt = new DateTime(dt.Year, dt.Month, 1, dt.Hour, dt.Minute, dt.Second);
                                                // Pass 1 for day

Upvotes: 2

Related Questions