0

I like to store the current datetime and store that in a variable called Entry date. When I do the above, I get the following message:

    var tz = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");

    var EntryDate = TimeZoneInfo.ConvertTimeToUtc(DateTime.Now, tz); 

I get the following message:

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.

I tried a number of ways to put Kind but was not successful

Nate Pet
  • 44,246
  • 124
  • 269
  • 414
  • UTC is the same anywhere in the world, regardless of the timezone. So this code is pointless, just use DateTime.UtcNow – Hans Passant Jun 06 '14 at 16:38

4 Answers4

1

change your code to something like this there are several ways to get the UTC date here are 3 different ways to the UTC Date/Time

var locDateTimeStr = DateTime.Now.ToString();
DateTime convertedDate = DateTime.SpecifyKind(DateTime.Parse(locDateTimeStr),DateTimeKind.Utc);
var kind = convertedDate.Kind; // `Kind you are seeking`
DateTime dt = convertedDate.ToLocalTime();    

or

var utcDateNow = DateTime.Now.ToUniversalTime();

or

var utcDateNow2 = DateTime.UtcNow;       
MethodMan
  • 18,625
  • 6
  • 34
  • 52
0

I'm not sure about what you are trying to do.

If you want the UTC date, use DateTime.UtcNow directly.

ken2k
  • 48,145
  • 10
  • 116
  • 176
0

If you are trying to convert your local time to a different time zone (that's what is looks like from your post), use ConvertTime:

  var tz = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");  
  var EntryDate = TimeZoneInfo.ConvertTime(DateTime.Now, tz);
Chris Hinton
  • 866
  • 5
  • 15
0

It's unclear what your trying to do. The CLR date/time support is, IMHO, poorly thought out. It would help if UTC dates and time were provided by default and would then have to get converted to local representations (for some definition of "local"). That would be almost ... Unix-like. But I digress.

Anyway, this might help:

  • To get the current instant in time, expressed relative to the current Time Zone:

    DateTime     localNow        = DateTime.Now ;
    
  • To get the current instant in time, expressed as Zulu/GMT/UTC (Universel Temps Coordonné — not exactly idiomatic or grammatically correct French, but committees are good :^) at language butchery), you can say

    DateTime     utcNow = DateTime.UtcNow ;
    

    (Note that a UTC time is constant and isn't dependent on timezone. If you had computers scattered around the world and configured for the local timezone

    • Tokyo
    • New York City
    • Paris
    • London

    and they all executed the following line of code at exactly the same time:

    DateTime now = DateTime.UtcNow ;
    

    They would all have exactly the same value for now.

    If you've already got a local "now", you can say:

    DateTime     utcNow = localNow.ToUniversalTime() ;
    

Once you've got a "now" in terms of UTC, you can get the time in the time zone of choice by saying something like:

    TimeZoneInfo desiredTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time") ;
    DateTime     desiredLocalNow = TimeZoneInfo.ConvertTime( utcNow , desiredTimeZone ) ;

You'll note that if you try to convert desiredLocalNow to UTC, you'll get a different value for UTC time: this is because DateTime is **NOT** timezone-aware and itsKindis set toDatetimeKind.Local. To give things timezone awareness, you'll need to useDateTimeOffset`, something along these lines:

DateTimeOffset utcNow           = DateTimeOffset.UtcNow ;
DateTimeOffset localNow         = utcNow.ToLocalTime() ;

TimeZoneInfo   desiredTimeZone  = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time") ;
DateTimeOffset desiredLocalNow1 = TimeZoneInfo.ConvertTime( localNow , desiredTimeZone ) ;
DateTimeOffset desiredLocalNow2 = TimeZoneInfo.ConvertTime( utcNow   , desiredTimeZone ) ;

Hope this helps!

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135