locked
Issue on Posting UTC DateTime to Web API add up 1 day RRS feed

  • Question

  • User677115475 posted

    Hi,

    I'm trying to post a data that has UTC DateTime but when it's translated in WebAPI it's adds 1 day on it. I'm not sure what should be done

    JavaScript 

      $.ajax({
    
                        url: 'http://localhost:41449/Promotion/Calculate',                
                        type: 'POST',
                        dataType: 'json',
                        data: {
                            "PLAccountPromotionID": 158,
                            "BusinessAreaID": 1,
                            "AccountID": 28,
                            "UserID": 81,
                            "PromotionPLName": 'test me',
                            "Description": 'test me',                
                            "StartDate": '2016-04-14T00:00:00.000Z',
                            "EndDate": '2016-05-31T17:44:29.000Z',                             
                            "ProcessID":3,
                        },
                        xhrFields: {
                            withCredentials: true
                        },
                        success: function (result) {
                            console.log(result);
                          
                        },
                        error: function (xhr, textStatus, errorThrown) {
                            console.log('Error in Database');
                        }
                    });

    Web API Model

     public DateTime EndDate { get; set; }

    It seems that 2016-05-31T17:44:29.000Z is being converted to 2016-06-01 when it is passed to the model.

    Not sure on how to solve this issue. Any help would be greatly appreciated.

    Thanks.

    Thursday, April 28, 2016 8:12 AM

Answers

  • User527778624 posted

    Hi,

    Try posting date string without 'Z' in it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 28, 2016 9:24 AM
  • User36583972 posted

    Hi vinx1127,

    From your description, I found your data format is ISO8601 and have 'Z' time zone offset. The conversion time method in the Web API method is based on the local time zone. So have the Offset time. You can use the following methods to solve your problem.

    Method 1:

    Your time string should specify time zone (You need to know the time zone, which sends to your api). Converted to the specified time zone in your api method.

    Method 2:

    Use the ToDateTimeFromUTCISO8601 method to convert time.

    The following code in my project for your reference.

    protected void Button1_Click(object sender, EventArgs e)
            {
                //methods 1-----------
                string data = "2016-04-29T04:08:56.749Z";
                // Being British time zone
                TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
                // Becomes local time
                DateTime dtime = Convert.ToDateTime(data);    
                Response.Write(dtime.ToString() + "|Local|  <br /> ");
    
                // Converted to the specified time zone
                var gtmTime = TimeZoneInfo.ConvertTime(dtime, TimeZoneInfo.Local, zone);
                Response.Write(gtmTime.ToString() + "|EN Time| <br />");
    
                //method 2---------------------
                //The method of directly converted into the right time
                var dt = ToDateTimeFromUTCISO8601(@"2016-05-31T17:44:29.000Z");
                var utcDT = dt.Value.ToUniversalTime();
                Response.Write(utcDT.ToString() + "<br />");
                Response.Write(utcDT.ToString(@"yyyy-MM-dd\THH:mm:ss.fff\Z") + "<br />");
            }
    
            /// <summary>
            /// Time ISO format to the correct time
            /// </summary>
            /// <param name="dateTimeString"></param>
            /// <returns></returns>
            static DateTime? ToDateTimeFromUTCISO8601(string dateTimeString)
            {
                DateTime dt;
                var sucessed = DateTime.TryParseExact(dateTimeString, new string[] { @"yyyy-MM-dd\THH:mm:ss.fff\Z", "o" }, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out dt);
                return sucessed ? new DateTime?(dt) : null;
            }

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 29, 2016 3:25 AM

All replies

  • User527778624 posted

    Hi,

    Try posting date string without 'Z' in it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 28, 2016 9:24 AM
  • User36583972 posted

    Hi vinx1127,

    From your description, I found your data format is ISO8601 and have 'Z' time zone offset. The conversion time method in the Web API method is based on the local time zone. So have the Offset time. You can use the following methods to solve your problem.

    Method 1:

    Your time string should specify time zone (You need to know the time zone, which sends to your api). Converted to the specified time zone in your api method.

    Method 2:

    Use the ToDateTimeFromUTCISO8601 method to convert time.

    The following code in my project for your reference.

    protected void Button1_Click(object sender, EventArgs e)
            {
                //methods 1-----------
                string data = "2016-04-29T04:08:56.749Z";
                // Being British time zone
                TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
                // Becomes local time
                DateTime dtime = Convert.ToDateTime(data);    
                Response.Write(dtime.ToString() + "|Local|  <br /> ");
    
                // Converted to the specified time zone
                var gtmTime = TimeZoneInfo.ConvertTime(dtime, TimeZoneInfo.Local, zone);
                Response.Write(gtmTime.ToString() + "|EN Time| <br />");
    
                //method 2---------------------
                //The method of directly converted into the right time
                var dt = ToDateTimeFromUTCISO8601(@"2016-05-31T17:44:29.000Z");
                var utcDT = dt.Value.ToUniversalTime();
                Response.Write(utcDT.ToString() + "<br />");
                Response.Write(utcDT.ToString(@"yyyy-MM-dd\THH:mm:ss.fff\Z") + "<br />");
            }
    
            /// <summary>
            /// Time ISO format to the correct time
            /// </summary>
            /// <param name="dateTimeString"></param>
            /// <returns></returns>
            static DateTime? ToDateTimeFromUTCISO8601(string dateTimeString)
            {
                DateTime dt;
                var sucessed = DateTime.TryParseExact(dateTimeString, new string[] { @"yyyy-MM-dd\THH:mm:ss.fff\Z", "o" }, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out dt);
                return sucessed ? new DateTime?(dt) : null;
            }

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 29, 2016 3:25 AM