I couldn't find a .NET date format type to handle dates in format I need to process, they sometimes have time zone offset with +/- prefix followed by 3 digits for the number of minutes timezone is offset by. i.e. +11:00 is written as +660

Usage Example:


The function:

private DateTime GetDateTime(string input)
        int timezoneIndex = input.IndexOf('+');
        if (timezoneIndex < 0) { timezoneIndex = input.IndexOf("-"); }

        if (timezoneIndex >= 0)
            string utcTime = input.Substring(0, timezoneIndex - 1);
            string timezoneOperator = input.Substring(timezoneIndex, 1);
            string timezoneOffset = string.Format(
                    TimeSpan.FromMinutes(Convert.ToDouble(input.Substring(timezoneIndex + 1))).ToString("hh\\:mm"));

            return DateTime.ParseExact(
                string.Format("{0}{1}", utcTime, timezoneOffset),
            return DateTime.ParseExact(
  • \$\begingroup\$ DateTimeOffset? Represents a point in time, typically expressed as a date and time of day, relative to Coordinated Universal Time (UTC). \$\endgroup\$ – t3chb0t Dec 30 '16 at 5:26
  • \$\begingroup\$ @t3chb0t Except that all the .NET API's require time-zone formatting to be HHmm, not total minutes. \$\endgroup\$ – Der Kommissar Dec 30 '16 at 6:59
  • 3
    \$\begingroup\$ In addition to a code review, don't forget that this is the type of thing that needs extensive unit tests! There are plenty of subtle bugs potentially lurking here that even the most experienced programmer cannot be expected to catch by inspection alone. \$\endgroup\$ – Cody Gray Dec 30 '16 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.