The problem: the answer depends on the time zone. If you have to use ZonedDateTime, the specific // time zone may need to be 'UTC'.) Date-only values: legacy Date => Temporal.PlainDateĪ common bug arises from a simple question: what date (year, month, and day) is represented by this Date? timeZoneId, 'Asia/Shanghai' ) // (And if the legacy Date instance was accessed using the // getUTCFullYear(), getUTCMonth(), etc. ![]() toZonedDateTimeISO ( 'Asia/Shanghai' ) Īssert. Especially // in a server context, you would be getting this time zone from // elsewhere in the data that you are processing. getTime ( ) ) // Here's an example of using a particular time zone. In a browser, this // will be the user's time zone, but on a server the value may not // be what you expect, so avoid doing this in a server context. To use the system's local time zone, which corresponds to using // legacy Date's getFullYear(), getMonth(), etc. ![]() This is // confusing, so Temporal has a more explicit way to do this. When calling methods on a legacy Date instance, you must decide // whether you want that exact time interpreted as a UTC value // (using methods containing "UTC" in their names) or in the // current system time zone (using other methods). You will need to specify a time zone, because legacy Date only // stores an exact time, and does not store a time zone. toString ( ), '' ) // If you need a ZonedDateTime, use the toZonedDateTime() or // toZonedDateTimeISO() method of the resulting Instant. const legacyDate = new Date ( '' ) const instant = legacyDate. Here's how to convert legacy ECMAScript Date into a Temporal.Instant or Temporal.ZonedDateTime instance corresponding to the same instant in exact time. epochSeconds Converting between Temporal types and legacy Date Legacy Date => Temporal.Instant and/or Temporal.ZonedDateTime epochMilliseconds // Timestamp in Seconds * This is the No.1 voted question on Stack Overflow for dates in JS If you want both, use Temporal.PlainDateTime. Note that if you just want the date and not the time, you should use Temporal.PlainDate. toString ( ) // date and time in ISO 8601 format toString ( ) // returns the date in ISO 8601 date format // If you additionally want the time: ![]() plainDateISO ( ) // Gets the current dateĭate. * This is a popular question on Stack Overflow for dates in JS How to get the current date and time in the local time zone? /** Here's how they would look using Temporal. These are some of the most common tasks that people ask questions about on StackOverflow with legacy Date. polyfill/README.md Frequently Asked Questions Running cookbook files: see instructions in. Temporal Cookbook Overview Running the cookbook files Do try it and report bugs don't use it in production!
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |