Convert facebook date time to C# date time - c#

Hi i have notice there two date times facebook gives, one is a date time from Unix Epoch and other is RFC 3339 Datetime (if i am not wrong). I wanted to know what is the best way to convert between each other. I have tried DateTime.TryParse() method but some times it dosent return the correct parsed date. Like it gives 21-Dec-2010 7:21:56 AM for 2010-12-21T01:51:56+0000. But it seems it doesn't parse time correctly. So plese tell how to change between epoch times and above datetime in C# datetime.

DateTime.Parse is converting a UTC date to your local timezone.
To retrieve the original UTC date, call ToUniversalTime().

Related

How to convert date to ms (UTC) and vice-versa?

I'm making a C# windows form application. There is a DateTimePicker tool on the application and the tool's format is MM/dd/yyyy hh:mm:ss tt. For example, the date looks like this on the application: 07/28/2021 09:10:50 AM
I want to convert that date to milliseconds depending on UTC, and send that milliseconds to the server. I also need to do the opposite. When I receive the milliseconds from the server (UTC), I need to convert it to the format of the DateTimePicker (MM/dd/yyyy hh:mm:ss tt). I don't need to convert the date to the local time when I receive the milliseconds from the server. Everything will depend on UTC. How can I do that?
Use ParseExact to parse a specific format
https://learn.microsoft.com/en-us/dotnet/api/system.datetime.parseexact?redirectedfrom=MSDN&view=net-5.0#overloads
Be aware that depending on the tool/version using to accept the datetime, it may assume local-time/UTC so you may want to check the DateTime.Kind property.
https://github.com/dotnet/aspnetcore/issues/11584
If you are having trouble leverage DateTimeOffset to accept the value from the server and then pull out the raw DateTime from there using:
DateTimeOffset.DateTime

Convert in UTC time format in c#

I need to set expire date of product as present date till midnight as shown below in UTC format.
"2021-05-28T23:59:59Z"
How i can write this in C#.
Use DateTime.UtcNow.Date
DateTime.UtcNow.Date.AddDays(1).AddSeconds(-1).ToString("o")
The output of above is
2021-05-28T23:59:59.0000000Z

DateTime.ParseExact Failing to convert to correct date

I am using FullCalendar. Now there is a code where I get a Date/Time from FullCalendar API. This is called startDate and has a below format:
String. An ISO8601 string representation of the start date. It will
have a timezone offset similar to the calendar’s timeZone e.g.
2018-09-01T12:30:00-05:00. If selecting all-day cells, it won’t have a
time nor timezone part e.g. 2018-09-01.
Let us for this question, assume that startStr is 2020-11-15T23:00:00-08:00
I have an ASP.NET MVC backend, where I send this string using Javascript.
The server receives the correct string as above i.e. 2020-11-15T23:00:00-08:00
Now, there is a code where I parse this date to the DateTime object
DateTime sdt = DateTime.ParseExact(sstartStr, "yyyy'-'MM'-'dd'T'HH':'mm':'sszzz", CultureInfo.InvariantCulture)
But here, this date/time is parsed as 11/16/2020 12:30:00 PM.
(You can try parsing the said date using the above code, the output will be wrong as above.)
Am I missing something ?

Angular UTC Date is showing one day after

I am storing date and time separately from Angular to C#. While storing in database, I combine start date and start time field in C# and store it in dAtabase UTC like this: "2020-02-28T22:30:30Z".
While returning from c#, i create a new DateTime with start date and start time and return as one variable. However, if the date is 28/02/2020 and time is 4.00 am, with timezoneoffset of 5.30 India, the date gets rendered to 29/02/2020 4.00 am.
Is it possible to get date and time in Angular and render it separately as string etc.
Thanks
In JavaScript Date is a timestamp, counts the number of miliseconds since January 1, 1970 00:00 UTC. So you might be having a problem with timezones. Check this answer I gave a couple of weeks ago, it may guide you. I also add a function to solve it and some references there, Subtract day in conversion between Moment and LocalDate.
angular set timezone based on user browser timezone so you can change datetimes to another timezone. please read it How to convert Date in angular to another time zone

Dateime Parse method adding one day to input date

I am reading a date from an xml file and parsing it to a my desired format. It i adding a day to the date and i cant seem to figure out why.
input : 2014-02-12T15:21:19-08:00
output : 13 Feb 2014 01:21
Here is my code to parse date:
string date = DateTime.Parse(row["CountDate"].ToString()).ToString("dd MMM yyyy HH:mm");
Any help will be greatly appreciated.
The reason is that the timezone information is being used to adjust the time to your local time zone.
If you remove the "-08:00" suffix, you'll find that the time won't be adjusted. However, you need to know whether the timezone information is important before ignoring it!
Well it looks like what you have is a UTC date/time with an 8 hour offset, when you parse the date what you have is an instance of the local time (Parse will take into account the offset).
If you are only interested in the UTC date/time then you can parse just that particular information
DateTime.ParseExact("yyyy-MM-ddTHH:mm:ss", row["CountDate"],
CultureInfo.InvariantCulture);

Categories

Resources