英文:
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 '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)
答案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 & 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]
Now, about the date formats I did some changes:
Having disabled the serializeCheck option, now I can use .toDate() functions
Now wherever I need a date in a specific format I simply use a global function that receives two params: date & format
and using momentJS
the rest is easy.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论