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

3
  • Maybe 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 Commented Apr 26, 2017 at 6: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? Commented Apr 26, 2017 at 6:13
  • DateTimeOffset has a DateTime property - use it. Commented Apr 26, 2017 at 6:13

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.