Reputation: 1273
I have a DateTime.Utcnow that I want to convert to local time. I want to convert it to local time in the format of "dd/MM/yyyy HH:mm"
. ToLocalTime doesn't have a constructor for it.
_executionTimeRepository.SetLastSuccessfulRunDate(DateTime.UtcNow, "FailedJobService");
public void SetLastSuccessfulRunDate(DateTime successfulRunDate, string nameOfService)
{
using (var context = _contextFactory())
{
var field = context.ExecutionTimes.First(s => s.NameOfService == nameOfService);
field.LastRunDate = successfulRunDate.ToLocalTime();
context.SaveChanges();
}
}
Upvotes: 2
Views: 3403
Reputation: 20017
In case of String
string date1 = DateTime.UtcNow.ToLocalTime().ToString("dd/MM/yyyy HH:mm");
In case of DateTime
DateTime date2 = DateTime.ParseExact(DateTime.UtcNow.ToLocalTime().ToString("dd/MM/yyyy HH:mm"), "dd/MM/yyyy HH:mm", null);
Upvotes: 3
Reputation: 8276
Is field.LastRunDate
a DateTime
or string
? Depending on the type see options:
if DateTime
field.LastRunDate = successfulRunDate.ToLocalTime();
//and when displaying it convert to string with your format
string dateStr = field.LastRunDate.ToString("dd/MM/yyyy HH:mm");
if string
field.LastRunDate = successfulRunDate.ToLocalTime().ToString("dd/MM/yyyy HH:mm");
Upvotes: 0