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.

Moshi
  • 1,385
  • 2
  • 17
  • 36
  • 1
    Hope [Converting Times Between Time Zones](https://msdn.microsoft.com/en-us/library/bb397769(v=vs.110).aspx) might help you – Mohit S Jan 20 '17 at 08: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. – Abion47 Jan 20 '17 at 08:34
  • 1
    Better yet, use DateTimeInfo so you don't have to convert between offsets at all – Panagiotis Kanavos Jan 20 '17 at 08:43
  • Define "You know" , is the timezone difference available to the sender machine or you just know it as a developer ?? – Anestis Kivranoglou Jan 20 '17 at 08: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 – Panagiotis Kanavos Jan 20 '17 at 08:47
  • @AnestisKivranoglou your comment needs clarification. Abion47's comment is pretty clear - if you know that your application has to handle more than 1 timezone, use UTC only internally and convert to the user's offset in the end. I go one step further - use DateTimeInfo always, never assume the offset – Panagiotis Kanavos Jan 20 '17 at 08:49
  • @Moshii what would be the correct timezone for this string? The user's timezone? Or some offset stored per user? Browsers don't send any timezone information which is why [you need to determine the browser's tz offset using Javascript](http://stackoverflow.com/questions/13/determine-a-users-timezone). Or just use [date.toISOString()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) in Javascript – Panagiotis Kanavos Jan 20 '17 at 08:56
  • @Moshii states that he know the source timezone of the String. But does not clarify if this information is available from the system he works on. Example i have one legacy client (that you don't have access to source code) that runs on a local machine and sends those strings to your api. You know that the pc runs on a specific timezone, but the info is not sent by the client. Case 2 you have access to the code which generates the time string. – Anestis Kivranoglou Jan 20 '17 at 08:56
  • @AnestisKivranoglou Okay I have web application deployed in USA or other country. Web application crawl a webpage and extract time string. I know the time string is Bangladeshi Time Zone formatted. Now If I convert this time string like this `DateTime.Parse("2017-01-19 23:53:57")`, it gives me server's time zone formatted time. – Moshi Jan 20 '17 at 09:14
  • `DateTime.Parse("2017-01-19 23:53:57")` will not use the server's time zone. Or any time zone for that matter. It gives you back a `DateTime` which is exactly what you put in to it, having a `Kind` of `DateTimeKind.Unspecified`. – Matt Johnson-Pint Jan 20 '17 at 21:00
  • Time zone of the server is irrelevant unless you start relying on `DateTime.Now` or other `DateTimeKind.Local` usage. Don't do that, and time zone of the server won't matter at all. – Matt Johnson-Pint Jan 20 '17 at 21:02

2 Answers2

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);
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
Anestis Kivranoglou
  • 7,728
  • 5
  • 44
  • 47
  • 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. – Moshi Jan 20 '17 at 10:12
  • var time = DateTime.Parse("2017-01-19 23:53:57"); Your server parses to its local time zone but He knows that :) . – Anestis Kivranoglou Jan 20 '17 at 10:17
  • @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. – Matt Johnson-Pint Jan 20 '17 at 20:59
  • @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. – Moshi Jan 21 '17 at 07:28
-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);
    }
Mehmet
  • 739
  • 1
  • 6
  • 17
  • 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. – Panagiotis Kanavos Jan 20 '17 at 08:45