Replace the timeValue in a datetime insted of Add the time - c#

In a WPF Application, I have a DateTimePicker.
I save on a DateTime variable, the DateTimePicker value with a time.
dDate = DateTimePicker_Date.SelectedDate.Value.Add(TimePicker_Heure.Value.Value.TimeOfDay);
This code is Ok when dDate is null, but, when i update the DateTimePicker, the Time is Added.
So,if my DateTimePicker is : 1/2/2013 with 01:20.
At the first :
dDate = DateTimePicker_Date.SelectedDate.Value.Add(TimePicker_Heure.Value.Value.TimeOfDay);
All is ok, but, if i re-execute this line :
dDate = DateTimePicker_Date.SelectedDate.Value.Add(TimePicker_Heure.Value.Value.TimeOfDay);
The dDate value will be : 1/2/2013 with 02:40.
Could anyone tell me how solve this problem please?

Add to the Date property of the DateTime:
DateTimePicker_Date.SelectedDate.Value.Date.Add(
TimePicker_Heure.Value.Value.TimeOfDay);
The Date property has 00:00:00 time.

Related

DateTime returns wrong date

I'm trying to get today's date
DateTime todayDateTime = new DateTime();
and I'm getting this:
{1/1/0001 12:00:00 AM}.
Why is this happening?
Use this
DateTime date = DateTime.Now;
Using new DateTime() creates a DateTime with a time of "0".
If you want todays date you need to use DateTime.Today if you want a DateTime object with a date of today and a time of 12:00:00 AM or DateTime.Now if you want a DateTime with the day and time of the moment you called DateTime.Now.
According to MSDN, the constructor for DateTime which takes in a long initializes by using the specified number of ticks since January 1st, 0001, so saying new DateTime(0) yields this time, not the current time.
Instead, use the static field DateTime.Now to get a DateTime representing the current system time.
In your question you are just initializing the Variable todayDateTime but you have never assigned (set it). This is why it is date ("null")/ beginning of our time calculations.
To actually get todays Date, you can use the following:
DateTime today = DateTime.Today;
first of all you need to assigned a value in the datetime.
just use something like this :
DateTime today = DateTime.Today;

Textbox textmode is DateTimeLocal

I have a textbox and i would love it to be formatted. fortunately for me, i can get this done by changing the textmode = DateTimeLocal. Exactly what I want. Additionally, I would like to load this textbox with default values rather than leaving it with dd/mm/yy __:__:__ . I can change the text if it is a regular textbox (single mode) or even a datetime textbox. but i cannot change it with DateTimeLocal mode. some help please. thank you.
You will have to format the DateTime to a valid Date and Time string that can be parsed, here is one that works:
txtDateTimeLocal.Text = DateTime.Now.ToLocalTime().ToString("yyyy-MM-ddTHH:mm");
For more details on what other attributes you can set, see: http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#local-date-and-time-state-(type=datetime-local)
Try setting the whole datetime format including seconds and milliseconds, worked for me.
txtStartTime.Text = DateTime.Today.ToString("yyyy-MM-ddTHH:mm:ss.ss");
If you want to set a TextBox's DateTime dynamically from a database then you can use this code:
DataTable dtTemp = (DataTable)ViewState["DataSet"]; // this are temporary table
TextBox txtFromDate = (TextBox)gridEditBloackHomework.Rows[e.NewEditIndex].FindControl("txtFromDate");
TextBox txtToDate = (TextBox)gridEditBloackHomework.Rows[e.NewEditIndex].FindControl("txtToDate");
string c = dtTemp.Rows[e.NewEditIndex]["FromDate"].ToString();
DateTime FromDate = Convert.ToDateTime(dtTemp.Rows[e.NewEditIndex]["FromDate"]);
DateTime ToDate = Convert.ToDateTime(dtTemp.Rows[e.NewEditIndex]["ToDate"]);
DateTime dtFromDate = FromDate.AddHours(-2);
DateTime dtToDate = ToDate.AddHours(-2);
txtFromDate.Text = dtFromDate.ToLocalTime().ToString("yyyy-MM-ddTHH:mm").ToString();
txtToDate.Text = dtToDate.ToLocalTime().ToString("yyyy-MM-ddTHH:mm");

why is datetime not recording time when stored?

When recording a TimeStamp into the the table in the DB why does...
DateTime todaysDate = DateTime.Today;
BookingRecord newBooking = new BookingRecord();
//other code for adding record to db
newBooking.TimeStamp = todaysDate;
why does the record in timeStamp record as : 2013-09-04 00:00:00.000
I want the correct time to display as well?
thanks for reply
Because DateTime.Today is actually supposed to do that. Use DateTime.Now if you want the time information as well.
Edit: of course, it's recommended to use UTC DateTimes for storage in the database anyway, so it's probably best to use DateTime.UtcNow!
DateTime.Today will actually be today's date with the time portion set to 00:00:00:
http://msdn.microsoft.com/en-us/library/system.datetime.today.aspx
An object that is set to today's date, with the time component set to
00:00:00.
DateTime todaysDate = DateTime.Today;
change to
DateTime todaysDate = DateTime.Now;

How do I disregard the time portion when comparing two date values?

I want this to equate to false when the date displayed is today's date:
if (dateTimePickerScheduleDate.Value < DateTime.Now)
...but it doesn't, because the DTP's value contains "midnight" whereas "Now" is after midnight.
How do I "trunc" these values so that it disregards the time portion?
if(dateTimePickerScheduleDate.Value.Date < DateTime.Now.Date)
{
}
The Date property returns a DateTime reflecting only the Date component of your DateTime object with its time component zero'ed out.
DateTime.Now.ToShortDateString();

How to change the datetimepicker value automatically while in progress

I want to know is there any way to change the datetimpePicker value while in progress.
I am using this code to go the next day .
DateTime stdate = new DateTime();
stdate = dateTimePicker1.Value;
while (stdate <= DateTime.Now)
{
txtSelectedDate.Text = stdate.ToString("yyyyMMdd");
selectedDate = Convert.ToInt32(txtSelectedDate.Text);
stdate = stdate.AddDays(1);
}
I want to change the datetimepicker value while in progress automatically because my my code works on date value if i select the date which is not present in the files it does not works , if i change the datetimepicker value which the file haves it works fine.
I want to change the datetimepicker value while in progress automatically.
There would be great appreciation if someone coulde help me.
Thanks In Advance.
I'm not sure if I've understood your question but if you're looking to update the DateTimePicker after each increment, you just need to affect its Value property, something like this:
stdate = stdate.AddDays(1);
dateTimePicker1.Value = stdate;

Categories