May 20, 2021 WeChat Mini Program Development Document
Construct a reference to the service-side time. Can be used to query criteria, update field values, or add field values when records are added.
The method signature is as follows:
function serverDate(options?: object): ServerDate
The method accepts an optional object parameter option, whose fields are defined as follows:
The field name | Type | Required | The default | Description |
---|---|---|---|---|
offset | number | Whether | The referenced service-side time offset, in milliseconds, can be positive or negative |
The sample code
Set the field to service-side time when new records are added:
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
exports.main = async (event, context) => {
try {
return await db.collection('todos').add({
description: 'eat an apple',
createTime: db.serverDate()
})
} catch(e) {
console.error(e)
}
}
The update field is one hour after the service side time:
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
exports.main = async (event, context) => {
try {
return await db.collection('todos').doc('my-todo-id').update({
due: db.serverDate({
offset: 60 * 60 * 1000
})
})
} catch(e) {
console.error(e)
}
}
``