Вопрос:

Create time entry in Firestore with UTC 0

firebase google-cloud-functions google-cloud-firestore

637 просмотра

2 ответа

201 Репутация автора

I set a Firebase Cloud Functions which convert a date String into a time stamp in my Firestore database.

Problem is data is stored with UTC + 1

How can I set this string to be stored as UTC in Firestore.

Here is the Format of the string received from a third party provider.

2018-01-22 09:00:00

What Firestore currently return

Mon Jan 22 2018 09:00:00 GMT+0100 (CET)

Here is an abstract of my current function.

let activityContent = value.val()
let startDate = new Date(activityContent.start_date)
let endDate = new Date(activityContent.end_date)

collectionRef.set({
start_date: startDate,
end_date: endDate
})
Автор: Benoit Источник Размещён: 12.01.2018 12:47

Ответы (2)


1 плюс

3512 Репутация автора

What you can do to overcome his probelm is to convert the date to UTC and then send it to firestore:

function toUTC(date) {
    return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()));
}

let activityContent = value.val()
let startDate = new Date(activityContent.start_date)
let endDate = new Date(activityContent.end_date)

collectionRef.set({
start_date: toUTC(startDate),
end_date: toUTC(endDate)
})
Автор: atlanteh Размещён: 11.05.2018 07:13

0 плюса

73 Репутация автора

My problem was solved by using this code: GMT+6 (date.getHours()+6)

let itemDate = new Date(item.createdAt); /*createdAt: "2019-05-17 10:03:43.351"*/
let currDate = toUTC(new Date());
console.log("Every: " + itemDate+" : "+currDate);

 if (currDate > itemDate) {
 } else {
 }

 function toUTC(date) {
    return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours()+6, date.getMinutes(), date.getSeconds(), date.getMilliseconds()));
 }
Автор: Mustofa Kamal Размещён: 17.05.2019 04:09
Вопросы из категории :
32x32