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

WeChat small program cloud development service-side database 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 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)
  }
}
``