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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:24:14+00:00 2026-06-08T09:24:14+00:00

This code: public class PhotoDescriptor { public DateTime DateCreatedUtc { get; set; } }

  • 0

This code:

public class PhotoDescriptor
{
    public DateTime DateCreatedUtc { get; set; }
}

public class PhotosController : ApiController
{
    // GET api/photos
    public IEnumerable<PhotoDescriptor> GetListOfPhotos()
    {
        return new PhotoDescriptor[]
            {
                new PhotoDescriptor
                    {
                        DateCreatedUtc = DateTime.ParseExact(
                                "2012-07-24T00:28:41.8738770Z",
                                "o",
                                CultureInfo.InvariantCulture,
                                DateTimeStyles.None).ToUniversalTime(),
                    },
                new PhotoDescriptor
                    {
                        DateCreatedUtc = DateTime.ParseExact(
                                "2012-07-24T00:28:41.0000000Z",
                                "o",
                                CultureInfo.InvariantCulture,
                                DateTimeStyles.None).ToUniversalTime(),
                    },
            };
    }

returns the following JSON:

[{"DateCreatedUtc":"2012-07-24T00:28:41.873877Z"},
 {"DateCreatedUtc":"2012-07-24T00:28:41Z"}]

notice that trailing zeros were removed from the datetime. But when I’m trying to parse these strings to get my DateTime back, I get FormatException - String was not recognized as a valid DateTime:

var date = DateTime.ParseExact("2012-07-24T00:28:41.873877Z", "o", CultureInfo.InvariantCulture, DateTimeStyles.None);

which is correct, according to MSDN DateTime.ParseExact Method:

FormatException

s does not contain a date and time that corresponds to
the pattern specified in format.

and "o" format is defined as follows:

yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss’.’fffffffK

so clearly trailing zeros SHOULD be there.

Is it a bug in WebApi, or am I doing something wrong? And how should I pass date/time field to my .Net client?

Thank you.

  • 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-06-08T09:24:16+00:00Added an answer on June 8, 2026 at 9:24 am

    Yes, you’re parsing the hard-coded values appropriately – but your code shows nothing about how they should be written out in JSON. Your “should be” claim is only with respect to the format you’re parsing in, which of course isn’t propagated within the DateTime itself.

    If you want to parse the values yourself (and I have to ask why that’s not being done for you…) you can use F instead of f:

    DateTime value = DateTime.ParseExact(text, 
                                         "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'FFFFFFFK",
                                         CultureInfo.InvariantCulture);
    

    I’m not saying it isn’t a bug in WebApi – but I haven’t seen any indication that it is a bug in WebApi from your post… and Scott Hanselman’s post on dates in WebApi give examples which are “just down to the second”, suggesting that’s deliberate.

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

Sidebar

Related Questions

I have this code public class Room { public string Id { get; set;
Given this code: public class Car { public virtual int CarId { get; set;
I wrote this code: public class CustomData { public int F1 { get; set;
I am working with NHibernate and I get this code below: public class User
please consider this code : 1)public static class MyClass 2){ 3) public static DateTime
Take a look at this code : public class Program { [import]IMain Main {get;
I have a this code public class SomeClass<T>: IEnumerable<T> { public List<SomeClass<T>> MyList =
I have this code : public class OrderModel { public List<Order> Orders { get;
I have this code: public class Window extends JFrame { public Window(){ ... JButton
Take a look at this code: public class Test { public static void main(String...

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.