4

I am not entirely sure if this is even possible and I have tried a number of things, short of writing an algorithm.

I have a variable of type datetime which I want to convert into a specific timezone and into a specific format. Let me give an example

todayCloseLine -> UTC time -> "2021-05-25 18:36:00"

closeline_local -> converting todayCloseLine to GMT Standard Time -> "2021-05-25 19:36:00"

I was able to convert to the given timezone so no problems with that. What I want is this format for closeline_local

2021-05-25T18:36:00.0000000+01:00

What I tried is as below (amongst other things):

  • tried ToLocalTime().ToString("o") on todayCloseLine but for this to work my machine should be on the specific timezone. This is not an option

Now, what I can do is write an algorithm as follows:

  • get the difference in timespan depending on which one is greater
  • get the sign, + or - depending on which one is greater
  • then convert todayCloseLine to ToString("o") and append the sign and timespan to get the final output

But I want to avoid this and preferably use an inbuilt method or something to get this conversion

Please if you can help. Thanks in advance.

EDIT

These are the tries I have done. I tried permutations combinations in this but no luck

DateTime todayCloseLine1 = DateTime.SpecifyKind(Convert.ToDateTime("25-May-2021 18:36"), DateTimeKind.Utc);
var closeline_local1 = DateTime.SpecifyKind(TimeZoneInfo.ConvertTime(
todayCloseLine1,
TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time")), DateTimeKind.Local);
var dt1 = closeline_local1.ToLocalTime().ToString("o");

var dt2 = closeline_local1.ToString("yyyy-MM-dd'T'HH:mm:ss.fffffffzzz");

OUTPUT for both dt1 and dt2 - 
2021-05-25T19:36:00.0000000+05:30 --> machine date IST
2021-05-25T19:36:00.0000000+01:00 --> machine date LONDON

Hope this helps

samar
  • 5,021
  • 9
  • 47
  • 71
  • If your client accepts ISO8601 format, then it shouldn't matter what the offset part is, just send them a UTC time. – Neil Jun 03 '21 at 15:42
  • 4
    Not really sure what is holding you up here. Do you just mean you want `closeline_local.ToString("yyyy-MM-dd'T'HH:mm:ss.FFFFFFFzzz")`? – DavidG Jun 03 '21 at 15:43
  • Can you post your sample code? –  Jun 03 '21 at 15:53
  • @Neil we actually save todayCloseLine and closeline_local and then later use it for different purposes – samar Jun 03 '21 at 15:57
  • @DavidG your code converts to "2021-05-25T19:36:00-04:00" – samar Jun 03 '21 at 15:57
  • Then you need to show more code, -4:00 suggests the time zone is not GMT, it's more like somewhere in the Americas. – DavidG Jun 03 '21 at 16:07
  • @DavidG sorry my bad some problem with my computer.. the code you have given gives this output, "2021-05-25T19:36:00+01:00" when i set my machine timezone to LONDON and gives this output "2021-05-25T19:36:00+05:30" when i set my machine to India. I cannot have code which is machine dependent – samar Jun 03 '21 at 16:17
  • It sounds to me like you are trying to imply information from the TZ offset that shouldn't be there. 18:36+01:00 = 19:36Z Are you interpreting the offset as some sort of timezone locator? – Neil Jun 03 '21 at 16:19
  • @Neil yes sort of but it can be converted back to datetime type as well – samar Jun 03 '21 at 16:24
  • 1
    That's not possible, the `zzz` format specifier gives you the offset from UTC, so we really need to see the rest of your code to understand why you are getting times like this in the first place. – DavidG Jun 03 '21 at 16:26
  • Much safer to have a separate field for specifying the tz IMO. – Neil Jun 03 '21 at 17:09
  • @FictitiousCode added sample code in main question – samar Jun 03 '21 at 18:03
  • 1
    Use the `DateTimeOffset` class. It will convert to a string correctly containing the offset suffix. – Ben Voigt Jun 03 '21 at 19:50
  • @BenVoigt i tried but i was not able to get it working. Can you plz give a sample code as an answer? I will accept it if it gets working. – samar Jun 04 '21 at 06:19

1 Answers1

1

use DateTimeOffset to format the string in desired format

DateTime close_line_UTC = DateTime.SpecifyKind(Convert.ToDateTime("25-May-2021 18:36"), DateTimeKind.Utc);
var myTimeZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
var closeline_local = TimeZoneInfo.ConvertTimeFromUtc(close_line_UTC, myTimeZone);
TimeSpan utcOffsett = myTimeZone.GetUtcOffset(closeline_local);
DateTimeOffset dateTimeOffset = new DateTimeOffset(closeline_local, utcOffsett);          
var closeline_local_formatted = dateTimeOffset.ToString("yyyy-MM-dd'T'HH:mm:ss.fffffffzzz");

References

  1. DateTime.ToString Method

  2. DateTimeOffset.ToString Method

Akshay G
  • 2,070
  • 1
  • 15
  • 33
  • OP has already said this doesn't work in the comments. Also, you shouldn't post code without explanation. – DavidG Jun 03 '21 at 16:42
  • 1
    @DavidG You have used F and not f That’s why it is not working – Akshay G Jun 03 '21 at 16:46
  • This also seems to be dependent on machine time. will add code in main question – samar Jun 03 '21 at 17:27
  • 1
    Don't reinvent the `DateTimeOffset` class already in the .NET library. – Ben Voigt Jun 03 '21 at 19:50
  • This is very good gave upvote and accepted answer. I tweaked it a little. Removed "Convert.ToDateTime("25-May-2021 18:36")" and assigned this to a variable and used that variable in DateTimeOffset constructor instead of closeline_local. So something like this DateTime ori = Convert.ToDateTime("25-May-2021 18:36"); DateTime close_line_UTC = DateTime.SpecifyKind(ori, DateTimeKind.Utc); . . . DateTimeOffset dateTimeOffset = new DateTimeOffset(ori, utcOffsett); – samar Jun 04 '21 at 07:47
  • 1
    FWIW this is the builtin `"o"` format for DateTimeOffset, see https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings – Ben Voigt Jun 04 '21 at 16:08
  • 1
    You should also be able to skip the `closeline_local` and `utcOffsett` lines and just do `DateTimeOffset dto = TimeZoneInfo.ConvertTime(new DateTimeOffset(close_line_UTC), myTimeZone);` When you call `ConvertTime` for a `DateTimeOffset` input, it will automatically set the offset in the result. – Ben Voigt Jun 04 '21 at 16:12