Compare Time of DateTime

  • Hey All,

    I want to send messages between 6 AM to 7 PM on same.

    Can anybody suggest me the code for this?

    Thanks in adv

  • That's not clear we need more information about what you want to do in order to provide a helpful response

  • ok, let me explain it.

    I’m sending messages to all staff members.

    But I want to validate this when we are off.

    i.e. messages should not be sent except office hours.

    I know the logic.

    1.Get System Date

    2.Extract the current time from this.

    3.Compare it with first time i.e 6.00 AM

    4.Compare it with second time i.e 7.00 PM

    I’ve a code for this—

    Dim Now As DateTime DateTime.Now

    Dim NowTime As [String] = Now.ToLongTimeString()

    Dim pm As DateTime = "6:00:00 PM"

    Dim am As DateTime = "7:00:00 PM"

    If (NowTime > pm) And (NowTime < am) Then

    ‘Send Message

    Else

    ‘Block Message

    End If

    My problem is in line 3 and 4

    How to assign time in variables and compare it using if-else

    Thanks

  • Sorry i was expecting a T-SQL script, i havent done any VB for a while but it looks like you are on the right track

    you may have to add the current date onto your PM and AM variables so that they form a valid DateTime to compare against,

    rather than comparing on time alone as i don't think VB can handle TIME data types like SQL can.

  • Ashwin9 (3/16/2011)


    ok, let me explain it.

    I’m sending messages to all staff members.

    But I want to validate this when we are off.

    i.e. messages should not be sent except office hours.

    I know the logic.

    1.Get System Date

    2.Extract the current time from this.

    3.Compare it with first time i.e 6.00 AM

    4.Compare it with second time i.e 7.00 PM

    I’ve a code for this—

    Dim Now As DateTime DateTime.Now

    Dim NowTime As [String] = Now.ToLongTimeString()

    Dim pm As DateTime = "6:00:00 PM"

    Dim am As DateTime = "7:00:00 PM"

    If (NowTime > pm) And (NowTime < am) Then

    ‘Send Message

    Else

    ‘Block Message

    End If

    My problem is in line 3 and 4

    How to assign time in variables and compare it using if-else

    Thanks

    I think you are making harder than it needs to be. try something like this.

    if(DateTime.Now.Hour > 6) And (DateTime.Now.Hour < 19) Then

    You really don't need to create a DateTime variable to hold DateTime.Now. You also don't need to compare it to other DateTime values. You just need to look at the hours of Now.

    Hope that helps.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Actually, I've already done it in using .NET code but

    I want to do the same thing using javascript.

    I've something like this----

    function CompareDateTime()

    {

    var today = new Date();

    if(today > new Date(today.getFullYear(), today.getMonth(), today.getDate(), 6, 0, 0))

    {

    'add validation;

    }

    }

    And This

    <asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="CompareDateTime()" />

    I'm not able to assign 6 AM to variable and compare it with todays Time

  • We are WAY off topic for sql server central but once again you are way over thinking this. Keep it simple. 😀

    var today = new Date();

    if (today.getHours() > 6 && today.getHours() < 19)

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply