我有一个DateTime.Utcnow,我想转换为本地时间。我想以"dd/MM/yyyy HH:mm"
的格式将其转换为本地时间。 ToLocalTime没有它的构造函数。
_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();
}
}
答案 0 :(得分:1)
如果是String
string date1 = DateTime.UtcNow.ToLocalTime().ToString("dd/MM/yyyy HH:mm");
如果是DateTime
DateTime date2 = DateTime.ParseExact(DateTime.UtcNow.ToLocalTime().ToString("dd/MM/yyyy HH:mm"), "dd/MM/yyyy HH:mm", null);
答案 1 :(得分:0)
field.LastRunDate
是DateTime
还是string
?根据类型,请参阅选项:
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");