# Convert date time string with non standard timezone offset in C#

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:

GetDateTime("20161124181215.000000+660")


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(
"{0}{1}",
timezoneOperator,
TimeSpan.FromMinutes(Convert.ToDouble(input.Substring(timezoneIndex + 1))).ToString("hh\\:mm"));

return DateTime.ParseExact(
string.Format("{0}{1}", utcTime, timezoneOffset),
"yyyyMMddHHmmss.FFFFFFK",
CultureInfo.InvariantCulture);
}
else
{
return DateTime.ParseExact(
input,
"yyyyMMddHHmmss.FFFFFF",
CultureInfo.InvariantCulture);
}
}

• DateTimeOffset? Represents a point in time, typically expressed as a date and time of day, relative to Coordinated Universal Time (UTC). – t3chb0t Dec 30 '16 at 5:26
• @t3chb0t Except that all the .NET API's require time-zone formatting to be HHmm, not total minutes. – Der Kommissar Dec 30 '16 at 6:59
• 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. – Cody Gray Dec 30 '16 at 9:38