javascript - How to create instance of `Temporal.Instant` at specific date and time? - Stack Overflow

I’m struggling with JavaScript’s proposed new Temporal API. What I am trying to do should be straight-f

I’m struggling with JavaScript’s proposed new Temporal API. What I am trying to do should be straight-forward, yet I fail to find a convincing solution. I must be missing something.

The task is as follows: instantiate an object representation of an UTC datetime from variables for year, month, day, hour and minute.

My thinking is as follows:

  • we are talking UTC so I need a Temporal.Instant;
  • new Temporal.Instant() requires the timestamp in nanoseconds so that doesn’t work;
  • Temporal.Instant.from() requires a ISO datetime string, which would require me to generate a properly formatted piece of text from the five variables I have — this is possible but a bit of a hack and kinda defeating the purpose of using a datetime library;
  • Temporal.PlainDateTime.from() has the right design, as it accepts an object like { year, month, day, hour, minute };
  • so then all we need to do is creating an Instant from this PlainDateTime. This does not seem to be possible though? Other than through — once again — a datetime string or a timestamp in ns…?

This is silly! The use case here is super basic, and yet it’s not obvious (to me) at all how to address it.

I was expecting to be able to simply do something like: Temporal.Instant.from({ year, month, day, hour, minute });

Now the best I can e up with is: Temporal.Instant.from(year + '-' + String(month).padStart(2, '0') + '-' + String(day).padStart(2, '0') + 'T' + String(hour).padStart(2, '0') + ':' + String(minute).padStart(2, '0') + 'Z'); //

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744674251a4587232.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信