如何在React Native中格式化这个日期时间戳(秒=1676238124,纳秒=838000000)?

huangapple go评论69阅读模式
英文:

How to format this date Timestamp(seconds=1676238124, nanoseconds=838000000) in react native

问题

我正在从Firestore中获取一些数据,日期字段的格式是{seconds: XXXXX, nanoseconds: XXXXX}。 React Native抱怨说这种格式不是可序列化的值,所以对于这些字段,我这样做:

docData.createdAt.toLocaleString()

这给我提供了以下格式的日期:

Timestamp(seconds=13223213, nanoseconds=12312312)

当我想以类似ISOString或LocaleDateString的人类格式呈现这些日期字段时,它不让我这样做。

我尝试过new Date(createdAt),但那给我NaN。
我还尝试过createdAt.toDate() | createdAt.toISOString(),但它不识别这些函数。
我还尝试过Timestamp.fromDate(createdAt),但它也不起作用。

我想以DD/MM/YYYY HH:mm:ss的人类阅读格式获取我的日期。

这是一些代码片段:

获取数据:
我的一些尝试

我希望能得到一些关于如何解决这个问题的提示或想法。

英文:

Im fetching some data from firestore, the date fields are in the format of {seconds: XXXXX, nanoseconds: XXXXX}. React Native complains about this saying this format is not a serializable value so to those fields I do this:

docData.createdAt.toLocaleString()

That gives me dates in the following format:

Timestamp(seconds=13223213, nanoseconds=12312312)

When I want to render those date fields with a human format like ISOString or LocaleDateString or anything it doesnt let me.

I've tried to do new Date(createdAt) but that gives me NaN.
I've also tried createdAt.toDate() | createdAt.toISOString() it doesnt recognize the functions.
I've also tried Timestamp.fromDate(createdAt) but it doesnt work either.

I want to get my dates in a human reading format like DD/MM/YYYY HH:mm:ss

Here some pieces of code:

Fetching data:
Some of my attempts

I'd like to have some hints or ideas or how to approach this issue.

答案1

得分: 1

import moment from 'moment'; // <--- import this line

const timestamp = 1676238124;
const nanoseconds = 838000000;

const Date = moment.unix(timestamp).add(nanoseconds / 1000000, 'milliseconds');
const Fordate = Date.format('YYYY-MM-DD HH:mm:ss.SSS');
console.warn(Fordate)

英文:

****we can use this format ..... work for me ****

import moment from &#39;moment&#39;;  &lt;--- import this line 

const timestamp = 1676238124;
const nanoseconds = 838000000;


const Date = moment.unix(timestamp).add(nanoseconds / 1000000, &#39;milliseconds&#39;);
const Fordate = Date.format(&#39;YYYY-MM-DD HH:mm:ss.SSS&#39;);
console.warn(Fordate)

答案2

得分: 0

尽管上述答案有效,但这是我所做的:

问题是由于 Redux 中的 SerializableCheck [https://redux-toolkit.js.org/api/serializabilityMiddleware] 导致的,因此我决定将此中间件设置为 false,以免引发此错误。

这是关于这个主题的 StackOverflow 问题,我找到的[https://stackoverflow.com/questions/61704805/getting-an-error-a-non-serializable-value-was-detected-in-the-state-when-using]

现在,关于日期格式,我进行了一些更改:

在禁用 serializeCheck 选项之后,现在我可以使用 .toDate() 函数

现在,无论我在何处需要以特定格式的日期,我只需使用一个接收两个参数的全局函数:date &amp; format,并使用 momentJS,其余的都很容易。

英文:

Although the above answer works, this is what I did:

The issue was caused because of the SerializableCheck from redux [https://redux-toolkit.js.org/api/serializabilityMiddleware] so I decided to set this middleware to false so It won't cause this error.

This is a StackOverFlow question about this topic that I found [https://stackoverflow.com/questions/61704805/getting-an-error-a-non-serializable-value-was-detected-in-the-state-when-using]

如何在React Native中格式化这个日期时间戳(秒=1676238124,纳秒=838000000)?

Now, about the date formats I did some changes:

Having disabled the serializeCheck option, now I can use .toDate() functions

如何在React Native中格式化这个日期时间戳(秒=1676238124,纳秒=838000000)?

Now wherever I need a date in a specific format I simply use a global function that receives two params: date &amp; format and using momentJS the rest is easy.

如何在React Native中格式化这个日期时间戳(秒=1676238124,纳秒=838000000)?

huangapple
  • 本文由 发表于 2023年2月16日 08:12:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定