0

I'm trying to bring over facebook events to my website. They will be coming from different regions. For example, an event is in Eastern Time (-4 hours difference from UTC) and my local time is Central time (-5 hours difference from UTC). I am calling their graph API from a console app. I get the date of the events like this:

// get event items
me = fbClient.Get(url);
var startTime = me["start_time"];
var endTime = me["end_time"];

the start time shows: "2017-04-30T13:00:00-0400" object {string}

When I try to convert that string into a DateColumn type, it changes the output time to: var dateTime = Convert.ToDateTime(startTime); {4/30/2017 12:00:00 PM}

It shifted the hour from 13 -> 12, how do I convert the string into date using DateTime and not using DateTimeoffset?

This examples shows how to do it using DateTimeOffset, but I need mine in DateTime type? https://stackoverflow.com/a/19403747/1019042

Community
  • 1
  • 1
user1019042
  • 2,428
  • 9
  • 43
  • 85
  • Maybe http://nodatime.org/ has some usefull methods for doing this. I would think that you need to tell the datetime object that the time your working with is in UTC -4 – Webbanditten Apr 26 '17 at 06:07
  • `DateTime` can only store local time and UTC, not other time zones, that's why it's converting. Why do you want to avoid `DateTimeOffset`? Isn't this what it's for? –  Apr 26 '17 at 06:13
  • `DateTimeOffset` has a `DateTime` property - use it. – Ofir Winegarten Apr 26 '17 at 06:13

1 Answers1

2

You can use the DateTime property of DateTimeOffset like the answer accepted in the link you've provided.

Or, if you really like to do it just with DateTime you can if you cut the timezone from the string:

var dt = DateTime.ParseExact(startTime.Substring(0,19), 
                              "yyyy-MM-ddTHH:mm:ss", 
                              CultureInfo.InvariantCulture);
Ofir Winegarten
  • 9,215
  • 2
  • 21
  • 27