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

  • Home
  • SEARCH
  • 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 5949807
In Process

The Archive Base Latest Questions

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

I’m using the DateTimeWithZone struct that Jon Skeet posted at Creating a DateTime in

  • 0

I’m using the DateTimeWithZone struct that Jon Skeet posted at Creating a DateTime in a specific Time Zone in c# fx 3.5

This didn’t work exactly for my situation since it assumes that the DateTime passed in the constructor is the local time, and therefore converts it to Utc using the specified TimeZone.

In my case we will mostly be passing in DateTime objects already in Utc (since this is what we are storing) so we need to only perform the conversion if the source DateTime.Kind is not Utc.

Therefore I changed the constructor to:

    public DateTimeWithZone(DateTime dateTime, TimeZoneInfo timeZone, DateTimeKind kind = DateTimeKind.Utc) {
        dateTime = DateTime.SpecifyKind(dateTime, kind);
        utcDateTime = TimeZoneInfo.ConvertTimeToUtc(dateTime, timeZone);
        this.timeZone = timeZone;
    }

Here we have an optional Kind parameter that defaults to Utc.

However, running this code and passing a Utc DateTime generates the following exception:

The conversion could not be completed because the supplied DateTime did not have the Kind property set correctly. For example, when the Kind property is DateTimeKind.Local, the source time zone must be TimeZoneInfo.Local.

According to the docs (http://msdn.microsoft.com/en-us/library/bb495915.aspx):

If the Kind property of the dateTime parameter equals DateTimeKind.Utc and the sourceTimeZone parameter equals TimeZoneInfo.Utc, this method returns dateTime without performing any conversion.

Since both the input time and the timezone both have a Kind property of Utc then I would not expect to get this exception.

Have I misunderstood?

  • 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:18:11+00:00Added an answer on May 22, 2026 at 5:18 pm

    Like the MSDN docs say if you pass in a DateTime with the kind set to anything besides DateTimeKind.Utc and specify a TimeZone other than Utc the conversion function will throw an exception. That Must be what is happening here. In your code you should check if the DateTime is already in Utc and skip the conversion if it is.

    Also since the dateTime you are passing in will have a DateTime attached to it already you probably don’t need to pass in a separate Kind parameter.

    from the docs

    Converts the time in a specified time
    zone to Coordinated Universal Time
    (UTC).

    meaning that it converts from the time zone supplied to Utc

    the function throws an argument exception if:

    dateTime .Kind is DateTimeKind.Utc and
    sourceTimeZone does not equal
    TimeZoneInfo.Utc.

    -or-

    dateTime .Kind is DateTimeKind.Local
    and sourceTimeZone does not equal
    TimeZoneInfo.Local.

    -or-

    sourceTimeZone .IsInvalidDateTime(
    dateTime ) returns true.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into

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.