I have an instance of DateTime that I get from my database, I want to subtract it from DateTime.Now and find out if 4 hours were passed. How do I do that?
Also when should i use DateTime.UTCNow or DateTimeOffset
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
You can use the subtraction operator to get a
TimeSpan:As for whether you need
UTCNoworNow… it will depend on what happens to time zones when you fetch the data from the database.DateTimeis not terribly clear on this front 🙁If you can fetch the value as a
DateTimeOffsetto start with, then you can useDateTimeOffset.Nowinstead and it should be simpler to work out any time zone issues.