NodaTime: From server to client

2019-08-09 09:45发布

问题:

I am using NodaTime I am storing time information in the form of Ticks since epoch.

However, I want to pass the number of milliseconds since unix epoch back to the client browser, so I can construct a javascript Date() object. Is there not a way to do this?

If this method is out of the question, does anyone else have a better idea?

p.s. I'm tired of messing with strings because I always have to wonder how they will be interpreted.

回答1:

There are 10 000 ticks per millisecond, and NodaTime has a constant for this, viz NodaConstants.TicksPerMillisecond.

If you are store time in an Instant object, you can get the ticks via the Instant.Ticks property.

And if necessary, the ZonedDateTime can be converted to an Instant via the .ToInstant() method

PS: v1.2 claims to have additional Xml and JSON serialization capabilities - possibly this will help you format your js date.