0

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.

11
  • 1
    Hope Converting Times Between Time Zones might help you Commented Jan 20, 2017 at 8:27
  • If you know that your DateTime is going to be used in multiple locales, you should be using DateTime.UtcNow whenever possible and doing the conversion to local time only on the end user's computer when necessary. Commented Jan 20, 2017 at 8:34
  • 1
    Better yet, use DateTimeInfo so you don't have to convert between offsets at all Commented Jan 20, 2017 at 8:43
  • Define "You know" , is the timezone difference available to the sender machine or you just know it as a developer ?? Commented Jan 20, 2017 at 8:46
  • @Moshii you can't guess the timezone just by looking at the string. Is it local time, UTC or some other offset? WIthout the offset you can ONLY pick between UTC and local. Use a full ISO8601 string instead, including the offset Commented Jan 20, 2017 at 8:47

2 Answers 2

5

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);
Sign up to request clarification or add additional context in comments.

4 Comments

wow, it is working, I tested it in one of my servers. But I didn't understand why this is working. Because in this line var time = DateTime.Parse("2017-01-19 23:53:57");' , time should be converted with machine's TimeZone configuration. Then it is trying to convert Bangladesh Standard Time' format which should be wrong I guess.
var time = DateTime.Parse("2017-01-19 23:53:57"); Your server parses to its local time zone but He knows that :) .
@Moshii - This converts from Bangladesh time, to UTC. Is that what you wanted? In your question, you didn't say whether it was from something to Bangladesh, or from Bangladesh to something, and you didn't state what that something was either.
@MattJohnson 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.
-2

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);
    }

1 Comment

First, that format can be parsed directly with DateTime.Parse. Besides, DateTime.ParseExact can handle any format. Second, the question isn't about parsing at all. It's about converting between timezones.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.