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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:34:47+00:00 2026-05-18T19:34:47+00:00

I have an object, it has a DateTime property… I want to pass that

  • 0

I have an object, it has a DateTime property… I want to pass that object from an .ashx handler back to a webpage via AJAX/JSON… I don’t want to use 3rd party controls…

when I do this:

  new JavaScriptSerializer().Serialize(DateTime.Now);

I get this:

  "\/Date(1251385232334)\/"

but I want “8/26/2009” (nevermind localization… my app is very localized, so my date formatting assumptions are not up for debate in this question). If I make/register a custom converter

public class DateTimeConverter : JavaScriptConverter
{
    public override IEnumerable<Type> SupportedTypes
    {
        get { return new List<Type>() { typeof(DateTime), typeof(DateTime?) }; }
    }

    public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
    {
        Dictionary<string, object> result = new Dictionary<string, object>();
        if (obj == null) return result;
        result["DateTime"] = ((DateTime)obj).ToShortDateString();
        return result;
    }

    public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
    {
        if (dictionary.ContainsKey("DateTime"))
            return new DateTime(long.Parse(dictionary["DateTime"].ToString()), DateTimeKind.Unspecified);
        return null;
    }
}

then I get this result (since the return value of the custom serialize method is a dictionary):

{"DateTime":"8/27/2009"}

so now in my Javascript, instead of doing

somePerson.Birthday

I have to do

somePerson.Birthday.DateTime 

  or

somePerson.Birthday["DateTime"]

how can I make the custom converter return a direct string so that I can have clean Javascript?

  • 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-18T19:34:47+00:00Added an answer on May 18, 2026 at 7:34 pm

    JavaScriptSerializer can definitely do what you desire.

    It’s possible to customize the serialization performed by JavaScriptSerializer for any type by creating a custom converter and registering it with the serializer. If you have a class called Person, we could create a converter like so:

    public class Person
    {
        public string Name { get; set; }
        public DateTime Birthday { get; set; }
    }
    
    public class PersonConverter : JavaScriptConverter
    {
        private const string _dateFormat = "MM/dd/yyyy";
    
        public override IEnumerable<Type> SupportedTypes
        {
            get
            {
                return new[] { typeof(Person) };
            }
        }
    
        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            Person p = new Person();
            foreach (string key in dictionary.Keys)
            {
                switch (key)
                {
                    case "Name":
                        p.Name = (string)dictionary[key];
                        break;
    
                    case "Birthday":
                        p.Birthday = DateTime.ParseExact(dictionary[key] as string, _dateFormat, DateTimeFormatInfo.InvariantInfo);
                        break;
                }
            }
            return p;
        }
    
        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            Person p = (Person)obj;
            IDictionary<string, object> serialized = new Dictionary<string, object>();
            serialized["Name"] = p.Name;
            serialized["Birthday"] = p.Birthday.ToString(_dateFormat);
            return serialized;
        }
    }
    

    And use it like this:

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    serializer.RegisterConverters(new[] { new PersonConverter() });
    
    Person p = new Person
                {
                    Name = "User Name",
                    Birthday = DateTime.Now
                };
    
    string json = serializer.Serialize(p);
    Console.WriteLine(json);
    // {"Name":"User Name","Birthday":"12/20/2010"}
    
    Person fromJson = serializer.Deserialize<Person>(json);
    Console.WriteLine(String.Format("{0}, {1}", fromJson.Name, fromJson.Birthday)); 
    // User Name, 12/20/2010 12:00:00 AM
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have objects that has a DateTime property, how can i query for the
I have two tables: object that has object_id column and avalues that have object_id
I have an object with a property called name. This object has a sub
I have an object that has a list of abstract 'aninamls'. i.e. var animals
I have an object that has a multi-part key and I am struggling to
I have an object which has several properties that are set when the object
I have an object that has a child collection: class Person { public string
I have an object that has 3 separate Dictionaries. The value parameter for each
Lets say I have an object that has stringProp1, stringProp2. I wish to store
I have a Task object which has a property of Employee. Im trying to

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.