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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:10:14+00:00 2026-05-22T17:10:14+00:00

Is there a universal way to instruct the DataContractJsonSerializer to use UTC for dates?

  • 0

Is there a universal way to instruct the DataContractJsonSerializer to use UTC for dates? Otherwise, I have to add .ToUniversalTime() to all of my date instances. Is this possible? The reason is that date values are defaulting DateTimeKind.Local and adding offsets to the JSON result. Making the dates universal does the trick, but can it be done at a global level? 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-22T17:10:15+00:00Added an answer on May 22, 2026 at 5:10 pm

    There’s no way to do that directly at the global level – primitive types (such as DateTime) can’t be “surrogated”. A possible workaround is to use some kind of reflection along with a surrogate to change the DateTime fields (or properties) in an object when it’s being serialized, as shown in the example below.

    public class StackOverflow_6100587_751090
    {
        public class MyType
        {
            public MyTypeWithDates d1;
            public MyTypeWithDates d2;
        }
        public class MyTypeWithDates
        {
            public DateTime Start;
            public DateTime End;
        }
        public class MySurrogate : IDataContractSurrogate
        {
            public object GetCustomDataToExport(Type clrType, Type dataContractType)
            {
                throw new NotImplementedException();
            }
    
            public object GetCustomDataToExport(MemberInfo memberInfo, Type dataContractType)
            {
                throw new NotImplementedException();
            }
    
            public Type GetDataContractType(Type type)
            {
                return type;
            }
    
            public object GetDeserializedObject(object obj, Type targetType)
            {
                return obj;
            }
    
            public void GetKnownCustomDataTypes(Collection<Type> customDataTypes)
            {
            }
    
            public object GetObjectToSerialize(object obj, Type targetType)
            {
                return ReplaceLocalDateWithUTC(obj);
            }
    
            public Type GetReferencedTypeOnImport(string typeName, string typeNamespace, object customData)
            {
                throw new NotImplementedException();
            }
    
            public CodeTypeDeclaration ProcessImportedType(CodeTypeDeclaration typeDeclaration, CodeCompileUnit compileUnit)
            {
                throw new NotImplementedException();
            }
    
            private object ReplaceLocalDateWithUTC(object obj)
            {
                if (obj == null) return null;
                Type objType = obj.GetType();
                foreach (var field in objType.GetFields())
                {
                    if (field.FieldType == typeof(DateTime))
                    {
                        DateTime fieldValue = (DateTime)field.GetValue(obj);
                        if (fieldValue.Kind != DateTimeKind.Utc)
                        {
                            field.SetValue(obj, fieldValue.ToUniversalTime());
                        }
                    }
                }
    
                return obj;
            }
        }
        public static void Test()
        {
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(MyType), null, int.MaxValue, true, new MySurrogate(), false);
            MyType t = new MyType
            {
                d1 = new MyTypeWithDates { Start = DateTime.Now, End = DateTime.Now.AddMinutes(1) },
                d2 = new MyTypeWithDates { Start = DateTime.Now.AddHours(1), End = DateTime.Now.AddHours(2) },
            };
            dcjs.WriteObject(ms, t);
            Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to read a collection of extension elements with Universal Feed
I wonder if there is a universal way of resolving a path using a
Since there is currently no universal way to read live data from an audio
In the below hierachy, is there a convenient and universal way to reference to
I am designing universal shift arithmetic operator. Is there a better way to achieve
Okay so is there a way to set a date that can be easily
I was wondering if there is a way to use Mod Rewrite to handle
Is there a universal way we can retrieve CellID in J2ME or what's the
Is there a universal approach in Python, to find out the path to the
I understand that there's no universal answer to the attribute vs. element debate (and

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.