Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core Replacement for ConvertTimeToUtc?

What happened to ConvertTimeToUtc in .NET Core? I know it's not there anymore, but has it been replaced by anything else? This is how I used to take a clients Date and Time input, and then convert it to UTC to store in database:

var timeZoneInfo=TimeZoneInfo.FindSystemTimeZoneById(viewModel.EventTimeZone);
var completeStartDate=TimeZoneInfo.ConvertTimeToUtc(viewModel.StartDate,timeZoneInfo);

This has worked great, until .NET Core. Any current examples I can find all seem to be using 'ConvertTime', but they are going from UTC to local, which is the opposite of what I am going for here. So can anyone out there tell me how I can accomplish this same feat in .NET Core? I am currently using this framework setup in my project.json file:

  "frameworks": {
        "netcoreapp1.0": {
            "imports": [
                "dotnet5.6",
                "portable-net45+win8"
            ]
        }
    },
like image 466
triggermouse Avatar asked Feb 01 '17 03:02

triggermouse


1 Answers

You can use TimeZoneInfo.ConvertTime and then choose destinationTimeZone (the third parameter) to be UTC:

var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(viewModel.EventTimeZone);
var completeStartDate =
    TimeZoneInfo.ConvertTime(viewModel.StartDate,timeZoneInfo, TimeZoneInfo.Utc);
like image 121
Ashkan Mobayen Khiabani Avatar answered Oct 31 '22 17:10

Ashkan Mobayen Khiabani