karthik80c
asked on
How do i Properly Convert Date time String in C#
Hi Experts ,
Am trying to convert the Date time field in asp web forms . but it showing error as "String was not recognized as a valid DateTime." .here is my code
Thanks Experts
Am trying to convert the Date time field in asp web forms . but it showing error as "String was not recognized as a valid DateTime." .here is my code
DateTime start = DateTime.ParseExact(txtStart.Text, "MM - dd - yyyy", null);
DateTime end = DateTime.ParseExact(txtEnd.Text, "MM - dd - yyyy", null);
and tried with Convert.ToDateTime method too shows same error . My value is 06-16-2016 .Please help on this experts .Thanks Experts
ASKER
hi let me try your method
Try
string dateTime = "01/08/2008 14:50:50.42";
DateTime dt = Convert.ToDateTime(dateTime);
ASKER
how can i convert both time and date time like this
string start = txtStart.Text + ""+ddlTimefrom.SelectedValue;
string end = txtStart.Text +" " + ddlTimeTo.SelectedValue;
DateTime dt = Convert.ToDateTime(start);
ASKER
hi HuaMinChen
it throws error as String was not recognized as a valid DateTime
it throws error as String was not recognized as a valid DateTime
you still use the DateTime.parse, but add the formatting string part for time...
https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx
for example, you could add this to the format string:
HH:mm:ss
it all depends on the input you are expecting
https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx
for example, you could add this to the format string:
HH:mm:ss
it all depends on the input you are expecting
Can you please post your code here?
ASKER
what i want is i want to convert both date and time into date time to store in database and my full code is
AddTask(Convert.ToInt32(ddlUser.SelectedValue),txtTask.Text, Convert.ToDateTime(Convert.ToDateTime(txtStart.Text).ToString("MM-dd-yyyy") + " " + ddlTimefrom.SelectedValue), Convert.ToDateTime(Convert.ToDateTime(txtEnd.Text).ToString("MM-dd-yyyy") + " " + ddlTimeTo.SelectedValue), Convert.ToInt32(txtReminderBefore.Text) * Convert.ToInt32(ddlReminderUnit.SelectedValue), ddlReminderUnit.SelectedItem.Text);
try it like this:
AddTask(Convert.ToInt32(dd lUser.Sele ctedValue) ,txtTask.T ext,
Convert.ToDateTime(txtStar t.Text+" "+ ddlTimefrom.SelectedValue) , Convert.ToDateTime(txtEnd. Text+" " + ddlTimeTo.SelectedValue),
Convert.ToInt32(txtReminde rBefore.Te xt) * Convert.ToInt32(ddlReminde rUnit.Sele ctedValue) , ddlReminderUnit.SelectedIt em.Text);
AddTask(Convert.ToInt32(dd
Convert.ToDateTime(txtStar
Convert.ToInt32(txtReminde
please try debugging and post what is value of
txtStart.Text+" "+ ddlTimefrom.SelectedValue ?
txtStart.Text+" "+ ddlTimefrom.SelectedValue ?
Try this one
AddTask(Convert.ToInt32(ddlUser.SelectedValue),txtTask.Text,Convert.ToDateTime(txtStart.Text+ " " + ddlTimefrom.SelectedValue),Convert.ToDateTime(txtEnd.Text + " " + ddlTimeTo.SelectedValue), Convert.ToInt32(txtReminderBefore.Text) * Convert.ToInt32(ddlReminderUnit.SelectedValue), ddlReminderUnit.SelectedItem.Text);
ASKER
hi Ramkisan Jagtap
The values are
txtStart.Text = "06-09-2016"
ddlTimefrom.SelectedValue = "8:00 AM"
The values are
txtStart.Text = "06-09-2016"
ddlTimefrom.SelectedValue = "8:00 AM"
ASKER
Sorry, my mistake.
I have posted your code only. Please try this one.
I have posted your code only. Please try this one.
AddTask(Convert.ToInt32(ddlUser.SelectedValue),txtTask.Text,DateTime.Parse(txtStart.Text+ " " + ddlTimefrom.SelectedValue),DateTime.Parse(txtEnd.Text + " " + ddlTimeTo.SelectedValue), Convert.ToInt32(txtReminderBefore.Text) * Convert.ToInt32(ddlReminderUnit.SelectedValue), ddlReminderUnit.SelectedItem.Text);
ASKER
hi roma ,
still showing same error . Sorry
still showing same error . Sorry
Can you please share the error image file again with my code so I can get more clarity on it.
Try to break down your code so will come to know the exact point where the error is coming.
Try to break out your code as below and see the error. Actually, the same code is running fine at my side.
Try one more change : instead of selectedValue use ddlTimefrom.Text
Try to break down your code so will come to know the exact point where the error is coming.
Try to break out your code as below and see the error. Actually, the same code is running fine at my side.
Try one more change : instead of selectedValue use ddlTimefrom.Text
DateTime StartTime = DateTime.Parse(txtStart.Text + " " + ddlTimefrom.Text);
DateTime EndTime = DateTime.Parse(txtEnd.Text + " " + ddlTimeTo.Text);
AddTask(Convert.ToInt32(ddlUser.SelectedValue),txtTask.Text,StartTime,EndTime, Convert.ToInt32(txtReminderBefore.Text) * Convert.ToInt32(ddlReminderUnit.SelectedValue), ddlReminderUnit.SelectedItem.Text);
Try to check ddlTimefrom.SelectedValue and ddlTimeTo.SelectedValue while debugging your code by keeping break point over there.
You might be getting null that's why you are get that error of conversion.
You might be getting null that's why you are get that error of conversion.
ASKER
Can you please show me the values of txtStart.Text and ddlTimefrom.Text while debugging.
I just want to confirm that these controls must contain the values at that point.
Just put one messagebox and take the screenshot.
Keep it inside button click at the first position.
I just want to confirm that these controls must contain the values at that point.
Just put one messagebox and take the screenshot.
Keep it inside button click at the first position.
MessageBox.Show("Date : " + txtStart.Text + " and Time : " + ddlTimefrom.Text);
I tried it as below:
string date="";
string time="";
DateTime dt= Convert.ToDateTime(date+ " " + time);
it works fine.
string date="";
string time="";
DateTime dt= Convert.ToDateTime(date+ " " + time);
it works fine.
ASKER
Sure Roma . Please see the screenshot
1.png
1.png
sorry,
I tried it as below:
string date="06-09-2016";
string time="8:00 AM";
DateTime dt= Convert.ToDateTime(date+ " " + time);
After that you can try it to convert to your required format.
I tried it as below:
string date="06-09-2016";
string time="8:00 AM";
DateTime dt= Convert.ToDateTime(date+ " " + time);
After that you can try it to convert to your required format.
ASKER
Hi Ramkisan Jagtap
I tried your code . but getting same error. The date am getting from html in this order "mm-dd-yyyy". That's why its showing Error
txtStart.Text = "06-16-2016"
ddlTimefrom.Text = "8:00 AM"
I tried your code . but getting same error. The date am getting from html in this order "mm-dd-yyyy". That's why its showing Error
txtStart.Text = "06-16-2016"
ddlTimefrom.Text = "8:00 AM"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you all Experts . Its working now . Special thanks to Ramkisan Jagtap and ROMA CHAUHAN
You're welcome :)
ASKER
please check the date format that you are getting from the html page and then try to convert it
ASKER
Where can i find best resources to learn for date and time function ?
06-16-2016
then your format must be:
MM-dd-yyyy
and not
MM - dd - yyyy
extra spaces are not acceptable
you may also try to "permit" spaces in the input like this:
Open in new window