ISO date time issue

by Manish Gupta   Last Updated August 14, 2019 10:24 AM

i have a web application with calender control if i am chosing 15 of Aug 2019 and submitting the data i am receiving the date in my web api as 2019-08-14T18:30:00Z then i am converting the datetime as follow string[] formats = { "yyyy-MM-ddTHH:mm:ssZ" }; var parsedDate = DateTime.ParseExact("2019-08-14T18:30:00Z", formats, CultureInfo.InvariantCulture, DateTimeStyles.None).ToString();

which is converting and giving me the desire date that is 08/15/2019 12:00:00 AM but while saving into the sql server db it is getting saved as 08/14/2019 12:00:00 AM

there is a day difference in the actual date and the saved date. How to solve this issue. Below is the Web API code to save into the sql server DB.

public IHttpActionResult Insert(ODataActionParameters parameters)
    {

        ResponseObject dataOp = new ResponseObject();          
        string[] formats = { "yyyy-MM-ddTHH:mm:ssZ" };

        var startDate = DateTime.ParseExact(parameters["StartDate"].ToString(), formats, CultureInfo.InvariantCulture, DateTimeStyles.None).ToString("dd-MM-yyyy");          

        try
        {               
            validToken = true;
            if (validToken)
            {
                    using (SqlConnection dbConnection = new SqlConnection(connectionString))
                    {
                        if (dbConnection != null && dbConnection.State == ConnectionState.Closed)
                        {
                            dbConnection.Open();
                        }

                        scCommand.Parameters.AddWithValue("@StartDate", parameters["StartDate"] == null ? DBNull.Value.ToString() : startDate);

                        scCommand.CommandType = CommandType.StoredProcedure;
                        scCommand.ExecuteNonQuery();

                        dataOp.TaskStatus = true;
                        dataOp.Message = "Inserted Successfully start date= " + parameters["StartDate"].ToString();
                        dataOp.AddnMessage = message;                           

                    }                   

            }
            else
            {
                dataOp.TaskStatus = false;
                dataOp.Message = "Invalid Token";
                dataOp.AddnMessage = message;
            }
        }
        catch (Exception ex)
        {
            addLogs(ex.ToString());
            dataOp.TaskStatus = false;
            dataOp.Message = ex.Message + "\n" + message;
        }
        return Ok(dataOp);
    }
Tags : support bug api sql web


Related Questions




Why is my Stack Overflow account inaccessible?

Updated March 26, 2015 07:30 AM

Favourite tags?

Updated April 03, 2015 21:27 PM