Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program cloud development API constructs a reference to service-side time


May 20, 2021 WeChat Mini Program Development Document


Table of contents


db.serverDate

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 db = wx.cloud.database()
db.collection('todos').add({
  description: 'eat an apple',
  createTime: db.serverDate()
})

The update field is one hour after the service side time:

const db = wx.cloud.database()
db.collection('todos').doc('my-todo-id').update({
  due: db.serverDate({
    offset: 60 * 60 * 1000
  })
})
``