Parse date in c# the same way as it works in javascript..?
I'm developing an electron application with typescript and another backend application with c#. Now i'm trying to parse data from .xlsx Files in my electron app (javascript), and almost the same kind of data trough an XML API from my c# application in the backend..
Now the thing is, the receiving data has a timestamp formatted like this:
2021-07-22T11:31:43Z
I don't even recognize this format (well i'm not having a P.h.D in date time formats.. who would want that anyways.. -.-)
Now, when i parse this date in javascript by doing this:
moment.locale('de')
const date = moment('2021-07-22T11:31:43Z', 'DD.MM.YYYY HH:mm:ss') // haha i'm not even using the correct format to parse this... moment still works magically
const myDate = date.format() // format without params will return ISO 8601, no fractional seconds
// myDate will be = "2021-07-22T11:31:43+02:00" - pure magic
See that? Javascript is automatically adding the timezone information of my personal computer i'm running the code with (and he adds this magic "+02:00" at the end..)
Now, i try to do the same thing with c#:
var timestampDate = DateTime.Parse("2021-07-22T11:31:43Z");
var myDate = timestampDate.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'");
// myDate will be = "2021-07-22T11:31:43.000Z"
Not even close! Well... i'm not mad at c# that he doesn't know what i want - i want the timezone information to be added from my local system (doesn't matter what the original was)...
The only thing that matters is - that both implementations are delivering the same value when the same input is given! How can i achieve this?
The only thing i know is: my JS implementation with moment is much more accurate than the c# one - why? Because if i save the data with this string "2021-07-22T11:31:43+02:00", i'll get the correct displaying value out in my Frontend (trough GraphQL) which is "11:31"..
If i test the same thing again with the data coming from c#, it will display "13:31"... which is totally wrong..
Any help very appreciated :)
from Recent Questions - Stack Overflow https://ift.tt/3BWYQww
https://ift.tt/eA8V8J
Comments
Post a Comment