Utcoffset formats in C# - c#

var utcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
Console.WriteLine(((utcOffset < TimeSpan.Zero) ? "-" : "+") + utcOffset.ToString("hhmm"));
The above code is working fine. But I need to display offset like +05:00. Is there any way to achieve this format?

From the docs:
The custom TimeSpan format specifiers don't include placeholder separator symbols, such as the symbols that separate days from hours, hours from minutes, or seconds from fractional seconds. Instead, these symbols must be included in the custom format string as string literals.
So you have to escape character in your format string that is not listed in the above page, either by surrounding it with ', or with a backslash, so:
utcOffset.ToString("hh':'mm")
However, you don't actually have to do this formatting yourself, if you format a DateTimeOffset, rather than TimeSpan. If you do this, you don't need all the "getting the UTC offset" mess either.
You just need the zzz Custom Format Specifier:
DateTimeOffset.Now.ToString("zzz")
You don't need all of the TimeZone stuff.

Instead of using TimeZone to look up the timezone from DateTime.Now, you can use DateTimeOffset.Now with the zzz format string and CultureInfo.InvariantCulture to achieve this:
Console.WriteLine(DateTimeOffset.Now.ToString("HHmmzzz", System.Globalization.CultureInfo.InvariantCulture));
// outputs 1255+02:00
Try it online
If you just want the offset in that format, you can use "zzz" instead of "HHmmzzzz".

Related

How to convert current date and time to "20181029093320-0400" this format in C#? [duplicate]

I'm working with a protocol that may optionally include a time zone offset when specifying datetime information. My code is written in C# and we are using the 4.0 .NET runtime. I see that there is a formatting option "zzz" for including timezone information when parsing and formatting, however, it appears that the colon (:) is fixed. For instance, a Datetime formatted with the custom format string (yyyyMMddHHmmsszzz) might appear as:
20100309101530-05:00
The protocol I am working with does not include the colon in the timezone offset. This protocol will format datetimes, and expect them to be formatted as:
20100309101530-0500
Is there a way to control the appearance of the colon when parsing or formatting datetime with a custom formatter that includes the timezone offset?
Doesn't look like there is anything built-in (you can use zz, but that leaves out the minutes).
You can roll your own by instantiating a DateTimeFormatInfo, setting TimeSeparator to string.Empty and using that as the IFormatProvider when calling DateTime.ToString (and make the call explicit, if it is not already).
But frankly, using Replace to remove the unwanted : from the default return value is so much easier.
I faced the same problem, ended up using an extension
public static class DateTimeExtensions
{
public static String ToSomeFormat(this DateTimeOffset dateTime)
{
return dateTime.ToString("yyyyMMddHHmmsszzz").Replace(":", "");
}
}
If you're using it in a place where it doesn't make sense to use replace or extend (for example something that might want to output as -05:00 with a colon when passed as zzz) and the minutes don't matter you could fake it with zz00.
var date = new DateTimeOffset(2008, 8, 1, 0, 0, 0, new TimeSpan(-5, 0, 0));
Console.WriteLine(date.ToString("yyyy-MM-dd-HH:mm:ss(zz00)"));
// outputs 2008-08-01-00:00:00(-0500)
I had exact same issue, trying to format/parse time zone in the format like +0730.
Formatting DateTime with zzz will always output time offset in hh:mm format.
Initially I though that it should be possible to get custom format by overriding DateTimeFormatInfo.TimeSeparator (: by default), but in case of the time zone it's hardcoded in System.DateTimeFormat.FormatCustomizedTimeZone:
// 'zzz*' or longer format e.g "-07:30"
result.AppendFormat(CultureInfo.InvariantCulture, ":{0:00}", offset.Minutes);
Here's response of engineer working on .NET:
The zzz is formatted as a time span and not regular DateTime. It has been decided since day one to use the standard format for the formatting the time zone part in the date/time objects. So, it is intentional always using : as a separator for the formatting the time span part. I understand you can argue this not the correct decision, but we cannot change this now as the scope of breaking will be very big as many people depend on parsing the dates assuming this format. You can still workaround this issue by replacing : with the desired separator you want.
So the here's my version of how to custom formatting of the timezone:
var utcOffset = dateTime.Kind == DateTimeKind.Utc? TimeSpan.Zero : TimeZoneInfo.Local.GetUtcOffset(dateTime);
var utcOffsetSrt = (utcOffset < TimeSpan.Zero ? "-" : "+") + utcOffset.ToString("hhmm", CultureInfo.InvariantCulture);
var dateTimeStr = dateTime.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture);
var result = dateTimeStr + utcOffsetSrt;
Parsing however works out of the box with zzz for empty separator:
var result = DateTime.ParseExact("+0000", "zzz", customInvariantCulture);
var resultOffset = TimeZoneInfo.Utc.GetUtcOffset(result.ToUniversalTime());
Assert.AreEqual(offset, resultOffset);
Looking into System.DateTimeParse.ParseTimeZoneOffset:
if (str.Match(":"))
{
// Found ':'
if (!ParseDigits(ref str, 2, out minuteOffset))
{
return false;
}
}
So zzz will work only if you have no delimiter or when it's :.

DateTime format not proper

i am trying to format the date to this format. 01/20/2013 02:30PM EDT, using this
LastModified.ToString("MM/dd/yyyy HH:mmtt");
but the result is coming like this
01-20-2013 02:30PM dont know why it is showing '-' instead '/'.
Also, for timezome, it seems there is only format available like +02:00. But I want timezone as string, i could not find any format for this, how can I get is as string like EDT/PST/IST etc?
From the MSDN page on custom date and time format strings:
The "/" custom format specifier represents the date separator, which is used to differentiate years, months, and days. The appropriate localized date separator is retrieved from the DateTimeFormatInfoDateSeparator property of the current or specified culture.
If you want it to definitely use /, you should either use the invariant culture, or quote the slash ("MM'/'dd'/'yyyy hh':'mmtt"). Note that I've quoted the time separator as well, as that can vary by culture too. I've also changed it to use the 12 hour clock, as per Arshad's answer.
When using a custom date/time format, you should probably use the invariant culture anyway. (For example, it seems odd to use a UK culture to format the string in a US-centric way - today would normally be represented as 02/05/2013 in the UK, not 05/02/2013.)
In terms of a time zone specifier - I don't know any way to use the time zone abbrevation within date/time formatting. I would personally advise against using abbreviations anyway, as they can be ambiguous and confusing. I can't see anything within TimeZoneInfo which even exposes that information for you to manually add it.
(It's possible that in Noda Time we'll support formatting with the abbreviation, but probably not parsing, precisely because of the ambiguity.)
i have found one mistake is that ,HH means time in 24 HRS format. You can try
string date = "01/20/2013 02:30PM";
DateTime dtTime;
if (DateTime.TryParseExact(date, "MM/dd/yyyy hh:mmtt",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out dtTime))
{
Console.WriteLine(dtTime);
}
Formatting of DateTime is influenced by your Culture and your format string. Your current culture (Thread.CurrentThread.CurrentCulture) uses the - as the default seperator of your date components.
The Culture of India uses - and since you are from Inda, it would make sence (see: http://en.wikipedia.org/wiki/Date_and_time_notation_in_India)
Two options:
Choose a correct Culture which uses the / by default as seperator. For example: LastModified.ToString("MM/dd/yyyy HH:mmtt", new CultureInfo("en-US"));
Or reformat your format string with the escape character \. For example: For example: LastModified.ToString("MM\/dd\/yyyy HH:mmtt");
See also: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Converting the WhenChanged attribute (Generalized-Time) in LDAP to a DateTime in C#

I recently switch from using S.DS namespace (which uses ADSI) to the S.SD.Protocol namespace. The only problem is that ADSI handled the conversion of Generalized-Time to a DateTime for me. Now I'm getting back a value of "20070828085401.0Z" for the WhenChanged attribute. DateTime.Parse() will not convert this so is there another way?
The format you are getting is close to the round trip date time pattern ("o") and universal sortable round trip date time pattern ("u") standard date time format strings as described here.
One kludgy solution would be to massage the string you get to fit the pattern and then use the "o" or "u" standard format string with ParseExact.
A better way would be to construct a custom format string that matches the data you are already getting. In the "How Standard Format Strings Work" section of the standard date time format strings page you'll see the full custom formatting strings equivalent to "o" and "u". That should give you a good start.
EDIT: Add code
string format = "yyyyMMddHHmmss.f'Z'";
string target = "20070828085401.0Z";
DateTime d = DateTime.ParseExact(target, format, CultureInfo.InvariantCulture);
In the comments lixonn observes that, using the format string above, ParseExact will not successfully parse a time string like 199412160532-0500.
It also won't parse a number of other valid strings such as times without the trailing 'Zulu' indicator (20070828085401.0); times without a fractional part (20070828085401Z) and times that represent minutes and seconds as a fractional hour (2007082808.90028Z).
The format string can be made slightly more forgiving by replacing the hard-coded 'Z' with the K custom specifier which will accept 'Z', an offset like -0500, and nothing. Whether that additional flexibility is a good thing will depend on your application.
Note that even with the K specifier Lixonn's string won't be parsed successfully since it lacks a fractional part to match the .f component of the format string.
You'll have to use DateTime.ParseExact() specifying the exact format.
You might have to play with the format a little bit but it would be something like this.
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;
string format="yyyyMMddhhmmss.0Z";
result = DateTime.ParseExact(dateString, format, provider);
You can use datetime's .strptime().
import datetime
# Since 0Z denotes UTC, you can get rid of it and apply the timezone
# later if you would like
time_string = "20070828085401.0Z".split('.')[0]
time_object = datetime.datetime.strptime(time_string, "%Y%m%d%H%M%S")
time_object should output as datetime.datetime(2007, 8, 28, 8, 54, 1). I believe it will be timezone naive, and equivalent to UTC time.
// WIN32 FILETIME is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).
// While the unix timestamp represents the seconds since January 1, 1970 (UTC).
private static long Win32FileTimeToUnixTimestamp(long fileTime)
{
//return fileTime / 10000L - 11644473600000L;
return DateTimeOffset.FromFileTime(fileTime).ToUnixTimeSeconds();
}
// The GeneralizedTime follows ASN.1 format, something like: 20190903130100.0Z and 20190903160100.0+0300
private static long GeneralizedTimeToUnixTimestamp(string generalizedTime)
{
var formats = new string[] { "yyyyMMddHHmmss.fZ", "yyyyMMddHHmmss.fzzz" };
return DateTimeOffset.ParseExact(generalizedTime, formats, System.Globalization.CultureInfo.InvariantCulture).ToUnixTimeSeconds();
}

parse iCalendar date using c#

Is there a direct way to parse an iCalendar date to .net using c#?
An iCalendar date looks like this:
2009-08-11T10:00+05:0000
I need to parse it to display it in a friendly format... thanks
string strDate = "2009-08-11T10:00+05:0000";
DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
dtfi.FullDateTimePattern = "yyyy-MM-ddTHH:mmzzz";
DateTime dt = DateTime.Parse(c.Substring(0, c.Length-2), dtfi);
zzz is for time zone, but is only recognized when expressed like this: +xx:xx.
I tested with your example, removing the last 2 0's then parsing with a custom DateTimeFormatInfo works.
You can use DateTime.Parse() to parse everything before the +. I do not know the iCalendar format specification but I assume after the + is the hours/minutes to add to the date before the +. So you could then use AddHours() and AddMinutes() to add the required bits to the DateTime returned by DateTime.Parse().
This requires a bit of string parsing but with a bit of regex you should be fine...
Since this is not a standard format string, but you know the exact format, you can use DateTime.ParseExact and specify a custom format string, like this:
DateTime.ParseExact(d, "yyyy-MM-ddTHH:mmzzz00", CultureInfo.InvariantCulture);
The 'zzz' specifier represents the hours and minutes offset from UTC, and the two concluding zeros are just literals to match format with which you're dealing.

DateTime.ParseExact string format exception

I am trying to convert a string into datetime with the following C# code,
DateTime dTo = DateTime.ParseExact(dateTo, "mm/dd/yyyy", CultureInfo.InvariantCulture);
eachtime I pass dateTo as 1/1/2010 it fails, instead it needs the string to be 01/01/2010.
What string format should I use to support both 01/01/2010 and 1/1/2010?
Using the following date format expression will allow you to use either single or double digit day and month elements.
"M/d/yyyy"
Note that the capital M is significant - a lower case m is the placeholder for minutes.
You will find more information related to date format strings here.
You can use the following Powershell command to test them.
[DateTime]::ParseExact('01/01/2010', 'M/d/yyyy', $null)
Capital M is month, little m is mins i think.
But to the point of the question, use Parse. ParseExact implies you know the exact format of the input.
You could try this format: MM/dd/yyyy, but I think there's no single format string that could support both inputs. You could test if the length of your dateTo string is less than 10 characters use M/d/yyyy, otherwise MM/dd/yyyy.

Categories