locked
Roll date if time entered is after midnight RRS feed

  • Question

  • User533367837 posted

    Hi again,

     In ASP.net, is there any elegant way to handle a set of time inserts from a form when the 2nd time is past midnight?

    Specifically, I have a form with 2 textboxes on it (startTime and endTime) that are set up to accept time values (using AJAX MaskedEditExtender for formatting/validation - pretty cool). This data is posted to a sub that enters the data into a table (T_Details). However, I've noticed that the data inserted as part of the record (SQL field is smalldatetime) doesn't take into account the fact that a time value past 23:59:59 in the "endTime" textbox is a time on the next day - it simply rolls to an A.M. date for the same day as the date for the pre-midnight value from the "startTime" textbox. 

    I'm sure that I can simply do some conditional coding and modify the date if necessary but is there a better way to do it? Thanks as always...this forum is a great resource [:D]

    Tuesday, February 19, 2008 8:47 PM

Answers

  • User472117617 posted

    Ok, you have some unclear business logic taking place.  You have to write the rules because only you know them.  For what time values do you want the date to be today's date?  For what values do you want the date to be tomorrow's date?  You will need to write code to do what you want.

    Here is an example of some code to this.  This code omits the second textbox and assumes that you want the earliest entry for "today" to be 3:00 AM.

     

    DateTime todayCutOff = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Date, 3, 0, 0);
    DateTime t1 = DateTime.Parse(time1.Text);              
    
    if (t1 < todayCutOff)
    {
         //12:00AM <= Time1 < 3:00AM
         t1 = t1.AddDays(1.0);
    }
    hope this helps! 
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 20, 2008 4:35 PM

All replies

  • User472117617 posted

    You have perfectly described the smalldatetime type.  You need to switch to a datetime.

    Hope this helps!

    Wednesday, February 20, 2008 1:03 AM
  • User533367837 posted

    Hi,

     Thanks for the reply but no joy - Time2 is still showing the same date as Time1 and in looking at it, I may be asking SQL to do something that it doesn't want to do. In submitting the times from text boxes, the date is simply being added onto the data by virtue of the "Convert.ToDateTime" declaration in my parameters - see below:

    objCommand.Parameters.AddWithValue("@Time1", SqlDbType.DateTime).Value = Convert.ToDateTime(Time1.Text)

    objCommand.Parameters.AddWithValue("@Time2", SqlDbType.DateTime).Value = Convert.ToDateTime(Time2.Text)

    No date is being entered by the end user, just the times so the code is appending the current date to it. This is a problem for me as the data may be entered retroactively - the user picks a date from a calendar object. I'll need to address that issue with code anyway by getting the picked date so I'm guessing that I will need to do some code work for the incremented date...unless I'm missing something - can you advise?

    Thanks...

    Wednesday, February 20, 2008 12:10 PM
  • User533367837 posted

    OK, so building on my last post, I need a way of comparing the values from Textbox "Time1" and Textbox "Time2" and if the value in "Time2" goes past 23:59 (00:30 for example), I need to increment the date for the SQL field "Time2" by one day - best approach (using VB.net)?

     Thanks...

    Wednesday, February 20, 2008 2:03 PM
  • User472117617 posted

    Ok, you have some unclear business logic taking place.  You have to write the rules because only you know them.  For what time values do you want the date to be today's date?  For what values do you want the date to be tomorrow's date?  You will need to write code to do what you want.

    Here is an example of some code to this.  This code omits the second textbox and assumes that you want the earliest entry for "today" to be 3:00 AM.

     

    DateTime todayCutOff = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Date, 3, 0, 0);
    DateTime t1 = DateTime.Parse(time1.Text);              
    
    if (t1 < todayCutOff)
    {
         //12:00AM <= Time1 < 3:00AM
         t1 = t1.AddDays(1.0);
    }
    hope this helps! 
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 20, 2008 4:35 PM
  • User533367837 posted

    Hi Matt,

     I think that will help some - will advise in the morning. As far as the business logic, in the grand scheme all i'm trying to do at this point is to look at the data in the second text box and make sure that it:

    1. ...is a later time than the data in the 1st textbox
    2. ...is a time value <=23:59
    3. ...if >23:59 (anything past midnight) then the date value that is inserted into the db table with that time is incremented by 1 day.

    Example: User enters 23:00 into textbox "Time1" and enters 00:30 into textbox "Time2". If Time2 is past 00:00 then upon the postback that calls the INSERT sub (in the code-behind) - I'm already doing some other data conversions in this sub so it seems logical to do this one as well.

    I may actually need to re-think this a bit. I can see some potential problems with users "fumble-fingering" the data in Time2 and the page thinking that it is a valid time frame, incrementing the date and inserting an inaccurate record into the table. This little project tracks flight times and aircraft utilization so the data has got to be accurate. In this example, if a user incorrectly enters 03:30 instead of 23:30, a 1/2 hour flight is turned into a 4 hour flight through an inaccurate end time value that the validation wouldn't catch because there was code that simply assumed the bad value in Time2 was actually a reference to time on the next day.

    Oh well, back to the drawing board - thanks anyway for your help...[:^)]

     

    Wednesday, February 20, 2008 7:35 PM
  • User472117617 posted

    The only solution is to ask the user to input the date as well. 

    Problems with your requirements:

    1. A time being later than another time reuqires a date component as time alone is circular (like a wall clock)
    2. all time values will be <= 23:59 (not counting seconds and milliseconds)
    3. because of #2, there is no time > 23:59 if you are not counting seconds.

     

    Wednesday, February 20, 2008 11:35 PM
  • User533367837 posted

    OK, well, it's not overly elegant but I added a checkbox to the page that users click if the time data traverses 00:00 and then built in code that looks for that checkbox data. If the box is checked, the date is incremented by one day (your code) and INSERTED into the table along with the time. A custom validator prevents the INSERT if the value in Time2 appears to be <= Time1 and the "overnight" checkbox is not checked.

     Thanks again for your help...[Yes]

    Thursday, February 21, 2008 10:55 AM
  • User773247133 posted

    This is realy not a good solution

    i also want to do same like this but i dont like your check box idea but could you just tell me after on wards which logic you applied if checkbox = true then

    Thanks

    Jalpa

    Thursday, May 5, 2011 8:21 PM