For the following code this will be the output (2020-06-05T23:02:24.554+05:30). I want the timezone without ":" in (+05:30) and expecting like (+0530). How to achieve this with datetime formatting? I can remove the colon with string operation but there should be some option in datetime format itself, Would be really good if someone suggest.
string date = String.Format("{0:s}.{0:fff}{0:zzz}", DateTime.Now);
There is no standard way of doing such, but you can write a customized method like with out testing that takes time and return utc as you asked for:
public static string GetUtc(DateTime dateTime)
{
return $"{dateTime:zzz}".Replace(":", "");
}
And call it
GetUtc(DateTime.Now);
This will return you +0530.
Btw I refer also to Get timezone from DateTime for reading as well.
You could use DateTimeOffset instead of DateTime to get what you're after.
var now = DateTimeOffset.Now;
string date = string.Format("{0}{1}{2}", now.ToString("s.fff"), now.Offset.Ticks < 0 ? "-" : "+", now.Offset.ToString("hhmm"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.