locked
Datetime from date and time RRS feed

  • Question

  • Hi

    I have a DatetTime EventDate that only contains date value and another DateTime EventStartTime that only contains time value. How can I merge the two into another DateTime EventStart to contain both date and time values?

    Thanks

    Regards


    • Edited by Y a h y a Saturday, August 15, 2015 10:45 AM
    Saturday, August 15, 2015 10:45 AM

Answers

  • If you look at the constructors of the DateTime Structure there are a few that will create a new Date with the specified year, month, day, hour, minute, and second.   You could create a New Date and specify the year, month, and day from one of your dates and the hour, minute, and second from the other date.

     As an example, place 2 DateTimePicker controls on a form and set the Format property of DateTimePicker2 to Time and try this code.

    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim dDate As Date = DateTimePicker1.Value
            Dim dTime As Date = DateTimePicker2.Value
    
            Dim d As New Date(dDate.Year, dDate.Month, dDate.Day, dTime.Hour, dTime.Minute, dTime.Second, DateTimeKind.Local)
            MessageBox.Show(d.ToString)
        End Sub
    End Class
    


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Saturday, August 15, 2015 11:21 AM
    • Proposed as answer by Frank L. Smith Saturday, August 15, 2015 11:37 AM
    • Marked as answer by Y a h y a Saturday, August 15, 2015 12:13 PM
    Saturday, August 15, 2015 11:10 AM

All replies

  • If you look at the constructors of the DateTime Structure there are a few that will create a new Date with the specified year, month, day, hour, minute, and second.   You could create a New Date and specify the year, month, and day from one of your dates and the hour, minute, and second from the other date.

     As an example, place 2 DateTimePicker controls on a form and set the Format property of DateTimePicker2 to Time and try this code.

    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim dDate As Date = DateTimePicker1.Value
            Dim dTime As Date = DateTimePicker2.Value
    
            Dim d As New Date(dDate.Year, dDate.Month, dDate.Day, dTime.Hour, dTime.Minute, dTime.Second, DateTimeKind.Local)
            MessageBox.Show(d.ToString)
        End Sub
    End Class
    


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Saturday, August 15, 2015 11:21 AM
    • Proposed as answer by Frank L. Smith Saturday, August 15, 2015 11:37 AM
    • Marked as answer by Y a h y a Saturday, August 15, 2015 12:13 PM
    Saturday, August 15, 2015 11:10 AM
  • If DateTimePicker1 has the date and DateTimePicker2 has the time then an alternative to IronRazer's approach.

            'combined DateTime
            Dim d As DateTime = New DateTime(DateTimePicker1.Value.Date.Ticks) 'get the date
            d = d.Add(DateTimePicker2.Value.TimeOfDay) 'add the time
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

    • Proposed as answer by IronRazerz Sunday, August 16, 2015 11:57 AM
    Sunday, August 16, 2015 11:50 AM