datetime.parse not working.

dateshit.JPG

everything looks right why is it still throwing that stupid error, i hate strings and date they need to do something regarding this :(
Aravind RanganathanWindows Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technology NinjaCommented:
Hi Aravind,

Please post your code using
 tags that makes it easier for experts to help you.

And I tried this code and it worked just fine.

[code]
string textDate = "04/01/2019 12:01:32 PM";
		DateTime myDate = DateTime.ParseExact(textDate, "MM/dd/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture);

Open in new window


Regards,
Chinmay.
Gustav BrockCIOCommented:
Parse will do it:

string textDate = "04/01/2019 12:01:32 PM";
DateTime trueDate =DateTime.Parse(textDate);

Open in new window

or, if you prefer to be specific:

string textDate = "04/01/2019 12:01:32 PM";
System.Globalization.CultureInfo provider = new System.Globalization.CultureInfo("en-US");
DateTime date =DateTime.Parse(textDate, provider);

Open in new window

Result:
2019-04-01 12:01:32

Open in new window

Aravind RanganathanWindows Application DeveloperAuthor Commented:
@Chimay Patel
here is the code

.aspx
    <script type="text/javascript">



        function ShowTime() {



            var dt = new Date();
            var n = dt.toLocaleDateString("en-029");
            var w = dt.toLocaleTimeString();
            var fullDate = n + ' ' + w;

            document.getElementById("<%= todaydate.ClientID %>").value = fullDate



        window.setTimeout("ShowTime()", 0);



    }



</script>

<script type="text/javascript">



    window.setTimeout("ShowTime()", 0);



</script>

Open in new window


.cs
  protected void btn_Create_Click(object sender, EventArgs e)
        {
            replaceblankvalueswithzero();
            using (SqlConnection db = new SqlConnection(con))
            {

                db.Open();
                string textDate = todaydate.Text.ToString();
                DateTime myDate = DateTime.ParseExact(textDate, "MM/dd/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture);
                //DateTime myDate = DateTime.ParseExact(todaydate.Text, "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
                string insert = "insert into dbo.Waste ([Plant],[Department],[Line],[Date],[Shift] ,[Operator] ,[Item_Code],[Item_Desc],[1st_Quality],[Un_coated_rework] ,[CoatedRework] ,[Purge_Out_UC_Rework],[Seconds] ,[TBD],[Floor_Waste] ,[Machine_Waste],[Purge_Out],[Roller_Scraper_Waste],[Extraordinary_Waste],[NOFCR],[Outdated],[Actual_Waste_Shipment],[Sub_Item],[Batch_ID],[Explanation_Details],[Approved],[Submited]) values ('" + PlantVal.ToString() + "','" + Department.SelectedValue.ToString() + "','" + LineValues.SelectedValue.ToString() + "','" + myDate + "','" + ShiftValues.SelectedValue.ToString() + "','" + Operator.Text + "','" + itemcode.Text + "','" + itemdesc.Text + "','" + firststQuality.Text + "','" + uncoated.Text + "','" + coatedrework.Text + "','" + purgeoutuc.Text + "','" + seconds.Text + "','" + tbd.Text + "','" + floorwaste.Text + "','" + machinewaste.Text + "','" + purgeout.Text + "','" + rollerscrapper.Text + "','" + extraordinary.Text + "','" + nofcr.Text + "','" + outdated.Text + "','" + actualwaste.Text + "','" + subitem.Text + "','" + batchid.Text + "','" + explanationdetails.Text + "','" + 0 + "','" + 1 + "')";
                SqlCommand cmd = new SqlCommand(insert, db);
                try
                {

                    int m = cmd.ExecuteNonQuery();
                    if (m != 0)
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + "Inserted into the database successfully" + "');", true); ;
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + "Data insert was not succesful please contact" + "');", true);
                    }
                }
                catch (Exception ex)
                {
                    PageLogger.Debug("DEBUG|" + ex.Message);
                    PageLogger.Error("ERROR|" + ex.StackTrace);
                    lblPopUpError.Text = ex.Message;
                }
            }
            PopulateGridview("empty", "empty");
            clearvalues();
        }

Open in new window

PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Aravind RanganathanWindows Application DeveloperAuthor Commented:
@Gustav Brock

Parse didnt work still getting the same error:

stupiddate.JPG
it_saigeDeveloperCommented:
Your filter is incorrect.  It needs to use: "MM/dd/yyyy hh:mm:ss tt".  You also need to pay mind to what Gustav has stated.  If your culture settings are not correct, then you won't be able to parse the date either.

Proof of concept -
using System;
using System.Globalization;

namespace EE_Q29141364
{
    class Program
    {
        static void Main(string[] args)
        {
            var date = default(DateTime);
            var culture = new CultureInfo("en-US");
            DateTime.TryParseExact("04/01/2019 12:01:32 PM", "MM/dd/yyyy hh:mm:ss tt", culture, DateTimeStyles.None, out date);
            Console.WriteLine(date);
            Console.ReadLine();
        }
    }
}

Open in new window

Produces the following output -Capture.PNG-saige-
Gustav BrockCIOCommented:
You are making it way too hard for yourself, as all you need is a string expression of the date value, and this should be ISO formatted, no AM/PM:

DateTime trueDate = DateTime.Now;
System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
string myDate = trueDate.ToString("yyyy-MM-dd HH:mm:ss", provider);
string insert = "insert into dbo.Waste …<snip> ..; 

Open in new window

Kelvin McDanielSr. DeveloperCommented:
The answer for your existing code is that the hours format should use the 12 hour string (hh) instead of the 24 hour string (HH) as both @Gustav and @saige have stated.

Further, I'm wondering why you are parsing the date at all as you're ingesting the exact same value the postback provides without validation or transformation. Looking at your debugger output the date appears to already be a string in the correct format; have you tried simply using that instead of parsing it?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.