SHEKHAR SHETE
SHEKHAR SHETE

Reputation: 6066

How to Combine Date & Time fields to DateTime in C#?

I have a TextBox Control on asp.net Form which inputs the Date From User. and another TextBox control to enter Time. Now, i want to Combine both these fields and save to DataBase.

Example: 05/25/1987+10.30PM

Results to 05/25/1987 10:30PM

Existing Code:

   public DateTime? DueDate { get; set; }

    DateTime? dd = new DateTime();
    if (!string.IsNullOrEmpty(txtDueDate.Text))
    {
        dd = Convert.ToDateTime(txtDueDate.Text.ToString().Trim());          
    }
    else
    {
        dd =null ;
    }
    objBB.DueDate = dd;

HTML markup:

<strong> Due Date:</strong>&nbsp; <asp:TextBox ID="txtDueDate" runat="server" CssClass="txtadmincms" Width="110px"></asp:TextBox>         
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDueDate">
 </cc1:CalendarExtender>

 <strong> Time:</strong>&nbsp; <asp:TextBox ID="txtTime" runat="server" CssClass="txtadmincms" Width="110px"></asp:TextBox>
<cc1:MaskedEditExtender ID="meeStartTimePunchIn" runat="server" AcceptAMPM="true"
   MaskType="Time" Mask="99:99" MessageValidatorTip="true" OnFocusCssClass="MaskedEditFocus"
  OnInvalidCssClass="MaskedEditError" ErrorTooltipEnabled="true" UserTimeFormat="None"
  TargetControlID="txtTime" InputDirection="LeftToRight" AcceptNegative="Left">
</cc1:MaskedEditExtender>

UPDATED: if TIME field TextBox is empty it gives error in else part

if (!string.IsNullOrEmpty(txtDueDate.Text))
        {

            dd = Convert.ToDateTime(txtDueDate.Text.ToString().Trim());
            if (!string.IsNullOrEmpty(txtTime.Text))
            {
                ddateTime = DateTime.ParseExact(txtDueDate.Text.ToString() + txtTime.Text.ToString(), "MM-dd-yyyy HH:mm", CultureInfo.InvariantCulture);
            }
            else
            {
                ddateTime = DateTime.ParseExact(txtDueDate.Text.ToString(), "MM-dd-yyyy HH:mm", CultureInfo.InvariantCulture);
            }
            dd = ddateTime;
            //Combine here date & time

        }
        else
        {
            dd =null ;
        }
        objBB.DueDate = dd;

Help Appreciated!

Upvotes: 0

Views: 2468

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460168

You can use DateTime.Parse or DateTime.ParseExact:

string dateAndTime = txtDueDate.Text.Trim() + ' ' + txtTime.Text.Trim(); // 05/25/1987 10:30PM
DateTime dt = DateTime.ParseExact(dateAndTime, "MM/dd/yyyy hh:mmtt", CultureInfo.InvariantCulture, DateTimeStyles.None);
dt = DateTime.Parse(dateAndTime, CultureInfo.InvariantCulture, DateTimeStyles.None);

Upvotes: 3

Nitin Varpe
Nitin Varpe

Reputation: 10694

Try this

  DateTime dt = 
        DateTime.ParseExact(txtDueDate.Text.ToString()+txtTime.Text.ToString(), 
        "MM-dd-yyyy HH:mm", CultureInfo.InvariantCulture);

Upvotes: 0

Andrei
Andrei

Reputation: 56698

You may try combining two strings into one and then parsing it:

string datePart = txtDueDate.Text.ToString().Trim();
string timePart = txtTime.Text.ToString().Trim();
string dateTime = string.Format("{0} {1}", datePart, timePart);
dd = DateTime.Parse(dateTime);

Couple of things to conside:

  • Make sure to provide format if needed
  • Check for empty values of textboxes
  • You might want to use TryParse instead of Parse for input validation

Upvotes: 2

Related Questions