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 4343954
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:49:38+00:00 2026-05-21T11:49:38+00:00

I have a date time in the database and I retrieve it from the

  • 0

I have a date time in the database and I retrieve it from the database using Entity Framework, I then pass out the data via JSON API through the DataContractJsonSerializer.

The time in the date time field appears to have been adjusted according to the local timezone of the server whilst being processed in DataContractJsonSerializer. The epoch expressed time is 1 hour ahead of the time expected. The DateTime Kind is UTC, but previously it was Unspecified and I had the same issue.

In my application, I wish to convert between timezones explicitly and on the client side, not the server, as this makes more sense. I’m surprised at this implicit functionality as my datetime values should be simple values just like an integer.

thanks

  • 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-21T11:49:39+00:00Added an answer on May 21, 2026 at 11:49 am

    DataContractJsonSerializer will output the timezone portion (+zzzz) if your DateTime.Kind is equal to Local OR Unspecified. This behaviour differs from the XmlSerializer which only outputs the timezone portion if Kind equals Unspecified.

    If curious check out the source for JsonWriterDelegator which contains the following method:

     internal override void WriteDateTime(DateTime value) 
        {
            // ToUniversalTime() truncates dates to DateTime.MaxValue or DateTime.MinValue instead of throwing 
            // This will break round-tripping of these dates (see bug 9690 in CSD Developer Framework)
            if (value.Kind != DateTimeKind.Utc)
            {
                long tickCount = value.Ticks - TimeZone.CurrentTimeZone.GetUtcOffset(value).Ticks; 
                if ((tickCount > DateTime.MaxValue.Ticks) || (tickCount < DateTime.MinValue.Ticks))
                { 
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( 
                        XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.JsonDateTimeOutOfRange), new ArgumentOutOfRangeException("value")));
                } 
            }
    
            writer.WriteString(JsonGlobals.DateTimeStartGuardReader);
            writer.WriteValue((value.ToUniversalTime().Ticks - JsonGlobals.unixEpochTicks) / 10000); 
    
            switch (value.Kind) 
            { 
                case DateTimeKind.Unspecified:
                case DateTimeKind.Local: 
                    // +"zzzz";
                    TimeSpan ts = TimeZone.CurrentTimeZone.GetUtcOffset(value.ToLocalTime());
                    if (ts.Ticks < 0)
                    { 
                        writer.WriteString("-");
                    } 
                    else 
                    {
                        writer.WriteString("+"); 
                    }
                    int hours = Math.Abs(ts.Hours);
                    writer.WriteString((hours < 10) ? "0" + hours : hours.ToString(CultureInfo.InvariantCulture));
                    int minutes = Math.Abs(ts.Minutes); 
                    writer.WriteString((minutes < 10) ? "0" + minutes : minutes.ToString(CultureInfo.InvariantCulture));
                    break; 
                case DateTimeKind.Utc: 
                    break;
            } 
            writer.WriteString(JsonGlobals.DateTimeEndGuardReader);
        }
    

    I’ve run the following test on my machine

    var jsonSerializer = new DataContractJsonSerializer(typeof(DateTime));
    var date = DateTime.UtcNow;
            Console.WriteLine("original date = " + date.ToString("s"));
            using (var stream = new MemoryStream())
            {
                jsonSerializer.WriteObject(stream, date);
    
                stream.Position = 0;
                var deserializedDate = (DateTime)jsonSerializer.ReadObject(stream);
                Console.WriteLine("deserialized date = " + deserializedDate.ToString("s"));
    
            }
    

    which produces the expected output:

    original date = 2011-04-19T10:24:39
    deserialized date = 2011-04-19T10:24:39
    

    Thus at some point your Date must be Unspecified or Local.

    After pulling it out of the DB convert the kind from Unspecified to Utc by calling

     entity.Date = DateTime.SpecifyKind(entity.Date, DateTimeKind.Utc);
    

    and don’t forget to assign the return value of SpecifyKind back into your object like I have

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

Sidebar

Related Questions

I have a database with DateTime fields that are currently stored in local time.
I have a date and time column in my mysql table called start_date and
I have an UPDATE sql command that modifies a Date/Time field in a particular
I have noticed that regardless of a given script's execution time, every date() call
hey, what do i have to write, if i want date&time which updates itself?
I am using Entity Framework with asp.net mvc, but I don't think mvc plays
I have a custom UserType which stores a date/time value in a TIMESTAMP field
I want to subtract between two date time values using SQL in MySQL such
I am getting a set of data from the database where the query produces
I am using java spring framework to develop a Webapp. I have a situation

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.