c# - 转换为本地时间格式

标签 c# .net datetime

我有一个 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();
    }
}

最佳答案

如果是字符串

string date1 = DateTime.UtcNow.ToLocalTime().ToString("dd/MM/yyyy HH:mm");

如果是日期时间

DateTime date2 = DateTime.ParseExact(DateTime.UtcNow.ToLocalTime().ToString("dd/MM/yyyy HH:mm"), "dd/MM/yyyy HH:mm", null);

关于c# - 转换为本地时间格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38046261/

相关文章:

c# - 嵌套 GridView - 如何触发子 GridView 按钮单击事件

c# - 有效文件权限工具的windows api

python - 从具有日期范围的 DataFrame 创建 Pandas 每日聚合时间序列

c# - 如何验证字符串是否正确的日期时间

C# - 内部静态只读在访问时为空

C# 如何在不知道您使用的是哪个 poco 的情况下将 Poco 转换为 List<Poco>

c# - 模拟子验证器时 FluentValidation 抛出 NullReference 异常

c# - 接受 SSL 套接字时性能非常差

c# - .net 中的 binning-bucketing 数值

sql - 带时区的 Postgres 时间戳转换