# db.serverDate
Constructs a reference to the server time, used to query a condition, update a field value, or add a record.
The method signature is shown as below:
function serverDate(options?: object): ServerDate
The method accepts an optional object parameter "options", whose field is defined as follows:
Field | Type | Required | Default | Description |
---|---|---|---|---|
offset | number | No | The offset of the server time referenced (in ms). It can be positive or negative. |
Sample code
When adding a record, set the field as the server time:
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)
}
}
Update the field to one hour later than the server 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)
}
}
``