Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1029861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:36:44+00:00 2026-05-16T12:36:44+00:00

A user enters a date in a form, but when I pass a date

  • 0

A user enters a date in a form, but when I pass a date through an AJAX json call, my date is getting changed by the DataContractJsonSerializer.

Here’s my AJAX call:

function Save()
{
    //Convert date to milliseconds from from 1/1/1970 - necessary for passing a date through JSON
    var d = "09/01/2010 05:00";
    myDate = new Date(d);
    myTicks = myDate.getTime();

    var message = 
    {
        MessageID: $("#MessageID").val(),
        MessageDate: '\/Date(' + myTicks + ')\/',
    };

    $.ajax({
        url: "<%= Url.Action("SaveMessage") %>",
        type: "POST",
        dataType: "json",
        data: $.toJSON(message),
        contentType: "application/json; charset=utf-8",
        success: function(result)
        {
            if (result && result.success)
            {
                //
            }
        }
    }); 
}

Here’s my serialization code:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    if ((filterContext.HttpContext.Request.ContentType ?? string.Empty).Contains("application/json"))
    {
        var serializer = new DataContractJsonSerializer(RootType);

        filterContext.ActionParameters[Param] = serializer.ReadObject(filterContext.HttpContext.Request.InputStream);
    }
}

It’s changing my dates to GMT. So if the user enters “09/01/2010 05:00”, after serialization it comes out as “09/01/2010 09:00”.

I was able to fix this in my local environment by using the function below, but it doesn’t work in production. In my local environment, the server time is set to EST (11:00 AM). On production, the server time is set to GMT (3:00 PM).

Here is my conversion function:

public DateTime ConvertToLocal(DateTime dt)
{
    int hours = TimeZoneInfo.Local.GetUtcOffset(dt).Hours;

    DateTime dtGMT = dt.AddHours(Convert.ToDouble(hours));

    return dtGMT;
}

This will correctly change my date from “09/01/2010 09:00 AM” back to “09/01/2010 05:00 AM” on my local environment, but on production it stays as “09/01/2010 09:00 AM”.

It seems like in production, the DataContractJsonSerializer realizes that I’m browsing from EST, so it adds 4 hours. But since my production server is in GMT, my ConvertToLocal doesn’t work, the Timezone offset in that function is 0 instead of -4.

What can I do to fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:36:44+00:00Added an answer on May 16, 2026 at 12:36 pm

    Instead of using myTicks = myDate.getTime(); you probably want to use myTicks = myDate.UTC();

    getTime() uses local time (based on the timezone where the browser is running). UTC() also gives you milliseconds since Jan 1, 1970, but it’s Jan 1, 1970 UTC instead of local time. Microsoft’s JSON serialization of dates uses the UTC measure.

    Then you shouldn’t need to do any server-side conversion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have form fields where the user enters in: percents: 50.5% money: $144.99 dates:
I have a form where user enters data about a new inventory item (id,title,
I want to validate the value a user enters in a text box, so
How would a go about making a program where the user enters a string,
I would like to intercept any URL which the user enters in their browser
In a Swing app a method should continue only after user enters a correct
I'm trying to write a C# program, where when a user enters some data
I have a timestamp the user enters in GMT. I would then like to
I let user enter some code in my Flex3 (Flash 10) app and I
i want something like this the user enter a website link i need check

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.