如何在time.zone.parse方法中包括毫秒



我正在尝试获取一个包括毫秒的时间对象:

x = Time.zone.parse("12-1-1998 00:00:00.000", "%Y-%m-%d %H:%M:%S.%3N") 

添加格式时会出现错误:

undefined method `year' for "%Y-%m-%d %H:%M:%S.%3N":String (NoMethodError)

我也尝试了Time.zone.parse("12-1-1998 00:00:00.000"),但我明白了:

1998-01-12T00:00:00Z

我需要时间看起来像这样:

1998-01-12T00:00:00.000Z

使用 DateTime#strftime

datetime = DateTime.strptime("2011-05-21 04:20:46.011", "%Y-%m-%d %H:%M:%S.%L")
datetime.strftime("%Y-%m-%dT%H:%M:%S.%LZ")
 => "2011-05-21T04:20:46.011Z"

最新更新