none
ISO date time conversion issue RRS feed

  • Question

  • -3
    <button aria-label="down vote" aria-pressed="false" class="js-vote-down-btn grid--cell s-btn s-btn__unset c-pointer" data-selected-classes="fc-theme-primary" style="margin:2px;box-sizing:inherit;font:inherit;padding:0px;border-width:initial;border-style:none;border-color:initial;border-radius:3px;background-image:none;background-background-size:initial;background-repeat:initial;background-attachment:initial;background-origin:initial;background-clip:initial;outline:none;box-shadow:none;" title="This question does not show any research effort; it is unclear or not useful"><svg aria-hidden="true" class="svg-icon m0 iconArrowDownLg" height="36" viewBox="0 0 36 36" width="36"><path d="M2 10h32L18 26 2 10z"></path></svg></button><button aria-label="favorite" aria-pressed="false" class="js-favorite-btn s-btn s-btn__unset c-pointer py8" data-selected-classes="fc-yellow-600" style="margin:0px;box-sizing:inherit;font:inherit;padding:0px;border-width:initial;border-style:none;border-color:initial;border-radius:3px;background-image:none;background-background-size:initial;background-repeat:initial;background-attachment:initial;background-origin:initial;background-clip:initial;outline:none;box-shadow:none;" title="Click to mark as favorite question (click again to undo)"><svg aria-hidden="true" class="svg-icon iconStar" height="18" viewBox="0 0 18 18" width="18"><path d="M9 12.65l-5.29 3.63 1.82-6.15L.44 6.22l6.42-.17L9 0l2.14 6.05 6.42.17-5.1 3.9 1.83 6.16L9 12.65z"></path></svg>
    </button>

    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);
        }

    Wednesday, August 14, 2019 9:38 AM

All replies

  • Maybe try two modifications:

       DateTime startDate = DateTime.ParseExact( . . . ).Date;        

       . . .

       scCommand.Parameters.AddWithValue("@StartDate", startDate);

    Wednesday, August 14, 2019 10:44 AM
  • Hi manish kumar gupt, 

    Thank you for posting here.

    For your question, I use a console application to make a sample on my side.

    I successfully insert the correct time in my database.

    Here’s the code of my test, and you can refer and modify it.

            static void Main(string[] args)
            {
                string str = "Sun 15 Jun 2008 8:30 AM -06:00";
                string format = "ddd dd MMM yyyy h:mm tt zzz";
                CultureInfo provider = CultureInfo.InvariantCulture;
                var startDate = DateTime.ParseExact(str, format, provider).ToString("dd-MM-yyyy");
    
                string connectionString = @"...";
                using (SqlConnection dbConnection = new SqlConnection(connectionString))
                {
                    dbConnection.Open();
                    string sql = "INSERT INTO newDate(StartDate) VALUES (@StartDate)";
                    SqlCommand scCommand = new SqlCommand(sql, dbConnection);
                    SqlParameter param = scCommand.Parameters.Add("@StartDate", SqlDbType.NVarChar);
                    param.Value = startDate;
                    scCommand.ExecuteNonQuery();
                }
            }

    Result of my test:

    Besides, if you have more questions about web application, I suggest you ask in ASP.NET forums for better help.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 15, 2019 8:55 AM
  • Hi,

    Thanks to reply but here is the problem 

    string str = "Sun 15 Jun 2008 8:30 AM -06:00";

    the str i am getting in this format 2019-08-14T18:30:00Z  one day before though the date is chosen  2019-08-15T18:30:00Z i am getting it as  2019-08-14T18:30:00Z and 14 Aug 2019 is geting saved i want to save as 15 aug 2019. do i need to add one day whatever i am getting?

    Thursday, August 15, 2019 10:52 AM
  • Hi manish kumar gupt,

    Thanks for your feedback.

    You can change your format of the time.

    here's my test:

                CultureInfo provider = CultureInfo.InvariantCulture;
                string str = "2019-08-15T18:30:00";
                var startDate = DateTime.Parse(str,provider).ToString("dd-MM-yyyy");

    Result:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 16, 2019 5:57 AM