I stored datetime into database then i retrieve the values.. My server db is located other country. so when the value is retrieve it is taking other country date and time..
I tried in controller as follow,
if (item.CreatedDate == null)
{
item.CreatedDate = DateTime.Now;
var timeZone = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(item.CreatedDate, TimeZoneInfo.Local.Id, item.CreatedDate.Value).ToString();
}
I got line error here... How to correct it?
You're passing values that aren't even of the right data type. Read up on
TimeZoneInfo
so you know how to use it properly.Also read:
DateTime.Now
Also understand that somewhere here you actually have to know the user's time zone. Just calling
TimeZoneInfo.Local.Id
isn't going to work, because that is also the server's time zone. There's no magic performed by MVC to know the time zone of your user. You'll have to ask them for it, or employ some other strategy involving JavaScript.From your comments, it appears you are looking for something like this:
Also, it seems like you might be using
DateTime?
(nullable datetimes) for your properties. It doesn't make sense to do that for these fields. They should be non-null in your database and always contain a value.