You were storing items as of India Standard Time, and then comparing them against the local time.
The solution you gave in your answer is adjusting the time back to India Standard Time, which is fixed at +5:30
offset from UTC. This only works because IST does not have any daylight saving time rules. If you were using a different time zone, such as US Eastern Time, it would not work reliably.
The correct solution is to store the original value as UTC. Instead of using DateTime.Now
and converting to IST, just use DateTime.UtcNow
and store the value directly. When you compare in your GetFriendlyDate
method, you should also use DateTime.UtcNow
as the basis for comparison.
If you already have data saved in your database in India Standard Time, then you will need to update those values when you make this change. For example, if this is SQL Server, you could run the following script to update your values from IST to UTC:
UPDATE mytable SET thedatetime = DATEADD(minute, -330, thedatetime)
In general, local date/time values such as those retrieved from DateTime.Now
have no business being in a web application. Read The Case Against DateTime.Now.