0

Actually my sqldatabase with Entity Framework save DateTime how date type local, but i need in the class model get that value ever like date type UTC. Exists one way from modify the getter method or a datanotation for change value type when i get it ? I thought in :

 public class ClassDatetime
{
    public DateTime DateLocal { get { return DateLocal.ToUniversalTime(); } set { } }
}

But it's a error by stack overflow exception....

Dr oscar
  • 359
  • 1
  • 4
  • 16
  • 2
    You code declares a property called `DateLocal`. The code for the getter of that property is `return DateLocal.ToUniversalTime();`, This will call your `DateLocal` property getter, which... will overflow your stack. Do you really want to have a property named DateLocal for the UTC date/time? – Flydog57 Jul 15 '20 at 20:57
  • Not, that code is only an example – Dr oscar Jul 15 '20 at 21:26
  • 1
    Well, we can't help you fix your code if you don't show us your code. The code you do show has a stack overflow in it. Your property getter code references the property you are defining. It will call itself forever and ever - until the stack overflows. What do you really want to do, provide read only access to the UTC version of the local time. If so, use something like `public DateTime DateUtc {get => DateLocal.ToUniversalTime();}` – Flydog57 Jul 15 '20 at 22:55
  • Not is for fix, only is a dude, if is possible change the value from a property directly in getter method ? – Dr oscar Jul 16 '20 at 02:53
  • 1
    You should not store local date and time. Save everything as UTC _or_ use `DateTimeOffset` _and_ a time zone identifier. – Aluan Haddad Jul 16 '20 at 18:48

0 Answers0