1

Given that DateTime.Kind is a get-only property, how do we assign DateTimeKind to a DateTime object that's born out of DateTime.Parse()?

var dte = DateTime.Parse("01/01/2018 10:10:00");

Console.WriteLine("{0} | Kind: {1}", dte, dte.Kind.ToString());

// PRINTS: 1/1/2018 10:10:00 AM | Kind: Unspecified

dte = dte.ToLocalTime();

Console.WriteLine("{0} | Kind: {1}", dte, dte.Kind.ToString());

// PRINTS: 1/1/2018 6:10:00 PM | Kind: Local
// OK, the "Kind" is changed to "Local" which is what I'm trying to achieve 
// however it also converted the time!!!

My question is how to apply Utc or Local "Kind" to the object dte without using dte.ToUniversal() or dte.ToLocal() because both method converts (or changes) the time

morethanyell
  • 316
  • 3
  • 10
  • 2
    Consider passing `DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal` for UTC time or `DateTimeStyles.AssumeLocal` for local time, to do this during the parsing phase rather than after parsing. – Matt Johnson-Pint Dec 29 '17 at 20:18

1 Answers1

1

You should be able to create a new DateTime with the DateTimeKind you want, by using the parsed datetime, and a constructor that takes DateTimeKind, like the following:
msdn.microsoft.com/en-us/library/t882fzc6(v=vs.110).aspx

ryanwebjackson
  • 1,017
  • 6
  • 22
  • 36
  • After my question was marked as duplicate, I found the answer (almost similar to your answer) from the link provided by Ivan Stoev. Thank you! – morethanyell Dec 29 '17 at 15:42