Moshi
Moshi

Reputation: 1433

Convert DateTime string to specific timezone DateTime in c#

I have a DateTime string and I know in which timeZone it is formatted but without any timeZone information in that string.

example : 2017-01-19 23:53:57

Now this string will be converted in server which is in another timeZone and I can not change timeZone of server.

If I use DateTime.Parse("2017-01-19 23:53:57"), I get DateTime of server machine's timeZone configuration.

This is my web application and server can be in different timezones.

I don't want to convert Bangladesh time to UTC. I just want to convert DateTime string which is Bangladesh time zone format to DateTime object also in Bangladesh time zone format.

Upvotes: 0

Views: 5960

Answers (2)

Anestis Kivranoglou
Anestis Kivranoglou

Reputation: 8204

This should do your work since you know explicitly that the source's time zone is in bagladesh.

var time = DateTime.Parse("2017-01-19 23:53:57");
var clientZone = TimeZoneInfo.FindSystemTimeZoneById("Bangladesh Standard Time");
var utcTime = TimeZoneInfo.ConvertTimeToUtc(time, clientZone);

Upvotes: 5

Mehmet
Mehmet

Reputation: 755

You can convert 2017-01-19 23:53:57 format string to datetime via below method.

DateTime DateConverter(string date)
    {
        string[] dateAndTimes= date.Split(' ');
        string[] dateParts = dateAndTimes[0].Split('-');
        string convertableString = dateParts[2] + "/" + dateParts[1] + "/" + dateParts[0] + " " + dateAndTimes[1];
        return Convert.ToDateTime(convertableString);
    }

Upvotes: -2

Related Questions