none
How to combine string Date and string time into DateTime

    Question

  • I’ m getting Date and Time as separate  string

    Date: “12/22/2012”

    Time: “10:00 AM”

    How do I combine this together into one C# DateTime field

    Thursday, December 13, 2012 10:29 PM

Answers

All replies

  • You can just concatenate the strings, and parse the results:

    DateTime result = DateTime.Parse(Date + " " + Time);

    (You could also parse the date, and then parse the time into a TimeSpan, and add those together, but I think the above is simpler.)


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, December 13, 2012 10:51 PM
  • and how do we combine two date field

    I have 

    public void Combine(DateTime date, DateTime time)
    {
    // how to merge in single datetime
    }


    Thursday, December 13, 2012 11:06 PM
  • and how do we combine two date field

    I have 

    public void Combine(DateTime date, DateTime time)
    {
    // how to merge in single datetime
    }


    Hi Lax,

    Welcome to the MSDN Forum.

    Try this way please:

    public void Combine(DateTime date, DateTime time)
    {
        DateTime dtddt = DateTime.Parse(date.ToString("dd/MM/yyyy") + " " + time.ToString("hh:mm tt"))
    }

    Thank you.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, December 14, 2012 9:54 AM
  • and how do we combine two date field

    I have 

    public void Combine(DateTime date, DateTime time)
    {
    // how to merge in single datetime
    }


    If you already have DateTime instances, I would use:

    public void Combine(DateTime date, DateTime time)
    {
       return new DateTime(date.Year, date.Month, date.Day, time.Hour, time.Minute, time.Second);
    }

    This avoids conversion to and from a string.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by cheong00 Tuesday, December 18, 2012 2:50 AM
    Friday, December 14, 2012 8:43 PM
  • Alternatively, you can use:

    public void Combine(DateTime date, DateTime time)
    {
       return date.Date + time.TimeOfDay;
    }

    This is probably my favorite option.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Friday, December 14, 2012 8:49 PM
  • What do you mean by merging two datetime. Can you tell the format of the desired output?
    Sunday, December 16, 2012 4:06 PM