Results 1 to 18 of 18

Thread: Resolved: Setting my start date with the time at 00:00:00

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Resolved: Setting my start date with the time at 00:00:00

    I am trying to get my start date set at the first day of the week and at 0 hours.
    vb Code:
    1. Select Case Now.DayOfWeek
    2.             Case DayOfWeek.Sunday : dtStart = DateTime.Now.AddDays(-0)
    3.             Case DayOfWeek.Monday : dtStart = DateTime.Now.AddDays(-1)
    4.             Case DayOfWeek.Tuesday : dtStart = DateTime.Now.AddDays(-2)
    5.             Case DayOfWeek.Wednesday : dtStart = DateTime.Now.AddDays(-3)
    6.             Case DayOfWeek.Thursday : dtStart = DateTime.Now.AddDays(-4)
    7.             Case DayOfWeek.Friday : dtStart = DateTime.Now.AddDays(-5)
    8.             Case DayOfWeek.Saturday : dtStart = DateTime.Now.AddDays(-6)
    9.         End Select

    Right now it is setting the date minus 5 days and using the current time (8/12/2007 8:50:14 PM) because I am using DateTime.Now. How can I set it so that it will come up as 8/12/2007 12:00:00 AM

    By the way I know there has to be a more elegent way to achieve what I am trying to accomplish but so far I have come up empty.
    Last edited by FastEddie; Aug 17th, 2007 at 10:01 PM. Reason: Resolved

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Setting my start date with the time at 00:00:00

    You can do something as simple as this:
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim dtStart As String = DateTime.Now.AddDays(-DateTime.Today.DayOfWeek).ToShortDateString & " 12:00 AM"
    
        MessageBox.Show(dtStart)
    
    End Sub

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Setting my start date with the time at 00:00:00

    DateTime.Now gives you the current date and time while DateTime.Today gives you the current date with time zeroed. DateTime.Today is equivalent to DateTime.Now.Date.
    vb.net Code:
    1. Dim dtStart As Date = Date.Today.AddDays(-Date.Today.DayOfWeek)
    2.  
    3. MessageBox.Show(dtStart.ToString("F"))
    Last edited by jmcilhinney; Aug 17th, 2007 at 08:55 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Setting my start date with the time at 00:00:00

    Thank you. This is what I ended up with.
    vb Code:
    1. Select Case Now.DayOfWeek
    2.             Case DayOfWeek.Sunday : dtStart = CDate(Date.Today.AddDays(-Date.Today.DayOfWeek))
    3.             Case DayOfWeek.Monday : dtStart = CDate(Date.Today.AddDays(-Date.Today.DayOfWeek))
    4.             Case DayOfWeek.Tuesday : dtStart = CDate(Date.Today.AddDays(-Date.Today.DayOfWeek))
    5.             Case DayOfWeek.Wednesday : dtStart = CDate(Date.Today.AddDays(-Date.Today.DayOfWeek))
    6.             Case DayOfWeek.Thursday : dtStart = CDate(Date.Today.AddDays(-Date.Today.DayOfWeek))
    7.             Case DayOfWeek.Friday : dtStart = CDate(Date.Today.AddDays(-Date.Today.DayOfWeek))
    8.             Case DayOfWeek.Saturday : dtStart = CDate(Date.Today.AddDays(-Date.Today.DayOfWeek))
    9.         End Select
    10.         tbMyStart.Text = (dtStart).ToShortDateString & " 12:00:00 AM"
    11.         tbMyEnd.Text = CStr(dtEnd)

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Setting my start date with the time at 00:00:00

    Quote Originally Posted by RhinoBull
    Why? There is really no need to...
    I'm not so sure. Your code creates a Date object with the correct date but the wrong time. It then converts that to a date string and tacks on an artificial time. You're left with a String object that provides the appropriate appearance, which is all well and good if you just want to display it to the user but I suspect that that's not all that's required. The OP has asked for a Date object with the time zeroed. You'll note that my code produces a date object with the correct date and the correct time. You can then convert that to a string if you want to display it but you can also use for other purposes, like assigning to a DateTimePicker, saving in a database or performing mathematical date calculations.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Setting my start date with the time at 00:00:00

    Quote Originally Posted by FastEddie
    Thank you. This is what I ended up with.
    vb Code:
    1. Select Case Now.DayOfWeek
    2.             Case DayOfWeek.Sunday : dtStart = CDate(Date.Today.AddDays(-Date.Today.DayOfWeek))
    3.             Case DayOfWeek.Monday : dtStart = CDate(Date.Today.AddDays(-Date.Today.DayOfWeek))
    4.             Case DayOfWeek.Tuesday : dtStart = CDate(Date.Today.AddDays(-Date.Today.DayOfWeek))
    5.             Case DayOfWeek.Wednesday : dtStart = CDate(Date.Today.AddDays(-Date.Today.DayOfWeek))
    6.             Case DayOfWeek.Thursday : dtStart = CDate(Date.Today.AddDays(-Date.Today.DayOfWeek))
    7.             Case DayOfWeek.Friday : dtStart = CDate(Date.Today.AddDays(-Date.Today.DayOfWeek))
    8.             Case DayOfWeek.Saturday : dtStart = CDate(Date.Today.AddDays(-Date.Today.DayOfWeek))
    9.         End Select
    10.         tbMyStart.Text = (dtStart).ToShortDateString & " 12:00:00 AM"
    11.         tbMyEnd.Text = CStr(dtEnd)
    That doesn't make a lot of sense, given that you're doing exactly the same thing for every Case. RB and I have both shown that it's just a single calculation that itself accounts for the different days of the week. Also, given that you ARE using Date.Today there's no point artificially adding your own time. Just use the time already part of your Date object:
    vb.net Code:
    1. Dim dtStart As Date = Date.Today.AddDays(-Date.Today.DayOfWeek)
    2.  
    3. MessageBox.Show(dtStart.ToString("G"))
    Last edited by jmcilhinney; Aug 17th, 2007 at 09:19 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Setting my start date with the time at 00:00:00

    Quote Originally Posted by jmcilhinney
    I'm not so sure. Your code creates a Date object with the correct date but the wrong time...
    You do not need time in this case - it's a fixed value so using Date only part is good enough. But you sure can talk for no reason.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Setting my start date with the time at 00:00:00

    Quote Originally Posted by RhinoBull
    You do not need time in this case - it's a fixed value so using Date only part is good enough. But you sure can talk for no reason.
    And what if you need to use this Date in a calculation? or save it to a database? Would you then parse that string back to a Date in order to get something that you could have had in the first place? Also, why would you choose to use Date.Now and tack on a time string when you can just use Date.Today on its own? Your solution may be OK but it's overall the poorer option for several reasons. No wink.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Setting my start date with the time at 00:00:00

    Oh I didn't realize I could do that all on one line (your example went right over my head the first couple of times). Now I have this:
    vb Code:
    1. Dim dtStart As Date = Date.Today.AddDays(-Date.Today.DayOfWeek)
    2.         tbMyStart.Text = (dtStart).ToShortDateString & " 12:00:00 AM"
    3.         tbMyEnd.Text = CStr(dtEnd)
    Now for some reason the " 12:00:00 AM" is not being added to the start date though Edit: I was overwritting my dtStart variable in another sub. Its all OK now.
    Last edited by FastEddie; Aug 17th, 2007 at 09:34 PM.

  11. #11
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Setting my start date with the time at 00:00:00

    Quote Originally Posted by jmcilhinney
    And what if you need to use this Date in a calculation?.
    The answer is very simple: you don't in this case. Suggestion was very specific to very specific question. [wink again]
    You use date/time when you need it - we only needed date here.

  12. #12
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Setting my start date with the time at 00:00:00

    Quote Originally Posted by FastEddie
    Oh I didn't realize I could do that all on one line (your example went right over my head the first couple of times). Now I have this:
    vb Code:
    1. Dim dtStart As Date = Date.Today.AddDays(-Date.Today.DayOfWeek)
    2.         tbMyStart.Text = (dtStart).ToShortDateString & " 12:00:00 AM"
    3.         tbMyEnd.Text = CStr(dtEnd)
    Now for some reason the " 12:00:00 AM" is not being added to the start date though.
    If you use what I posted "as is" you will get what you need.
    Some times I wonder about how difficult it could be to simply copy and paste?

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Setting my start date with the time at 00:00:00

    Quote Originally Posted by RhinoBull
    The answer is very simple: you don't in this case. Suggestion was very specific to very specific question. [wink again]
    You use date/time when you need it - we only needed date here.
    Nowhere in the original post does it say that a string representation of a date is required. The OP said they wanted a date and their code showed them creating a Date object. You assumed that a string was all that was needed. Maybe that assumption was correct. Post #5 suggests that it may be but as we haven't seen the rest of the code we still don't know for sure. Regardless of that, this:
    vb.net Code:
    1. DateTime.Today.AddDays(-DateTime.Today.DayOfWeek).ToString("G")
    is preferable to this:
    vb.net Code:
    1. DateTime.Now.AddDays(-DateTime.Today.DayOfWeek).ToShortDateString & " 12:00 AM"
    because it is more succinct. It may even be more efficient but that would be neither here nor there as the difference would be negligible.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Setting my start date with the time at 00:00:00

    Thanks RB. I was actually using cut and past originally but since I was resetting my variable further down in my code it wasn't apparent to me that either one of your examples were working.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Setting my start date with the time at 00:00:00

    Ooops I have to back up now. I am actualy doing two things with this date. One I am just setting the text properties of a textbox on my form but I am also passing this date to a Stored Procedure, which is now giving me this error.

    SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

    System.Data

  16. #16
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Setting my start date with the time at 00:00:00

    Quote Originally Posted by jmcilhinney
    Nowhere in the original post does it say that a string representation of a date is required...
    You know Jim, I sometimes wonder if you are from this planet?

    You have a good night or morning for that matter but keep me out of your pointless debates.

    Regards.

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Setting my start date with the time at 00:00:00

    I rest my case. A Date object is needed so you certainly should be creating a Date object with the appropriate time portion in the first place.

    As to your issue, I would guess that you're passing the information to your sproc incorrectly. Can you show us the sproc and also how you trying to save this data to your database?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Setting my start date with the time at 00:00:00

    I just figured it out.

    It is just tired and I am getting late.

    Thanks again for all the help and suggestions!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width