Skip to main content

Aggregate.dateToString

1. Operator Description

Function: Converts a date/time string to a date object.

Declaration: db.command.aggregate.dateToString({date: <date expression>,format: <format expression>,timezone: <timezone expression>,onNull: <null value expression> })

2. Operator Parameters

FieldTypeRequiredDescription
datestringRequiredDate expression
formatstringNoFormat expression
timezonestringNoTimezone expression
onNullstringNoNull value expression

The following is a detailed description of the four expressions:

NameDescription
Date expressionRequired. Specifies that the field value should be a date that can be converted to a string.
Format expressionOptional. It can be any valid string containing "format specifiers".
Timezone expressionOptional. Specifies the time zone for the operation result. It can parse strings in the format of UTC Offset or Olson Timezone Identifier.
Null value expressionOptional. Returns the value specified by this expression when the <Date expression> returns null or does not exist.

The following is a detailed description of format specifiers:

SpecifierDescriptionValid Values
%dDay of the month (2-digit, zero-padded)01 - 31
%GYear in ISO 8601 format0000 - 9999
%HHour (2-digit, zero-padded, 24-hour clock)00 - 23
%jDay of the year (3-digit, zero-padded)001 - 366
%LMilliseconds (3-digit, zero-padded)000 - 999
%mMonth (2-digit, zero-padded)01 - 12
%MMinutes (2-digit, zero-padded)00 - 59
%SSeconds (2-digit, zero-padded)00 - 60
%wDay of the week1 - 7
%uDay of the week in ISO 8601 format1 - 7
%UWeek of the year (2-digit, zero-padded)00 - 53
%VWeek of the year in ISO 8601 format1 - 53
%YYear (4-digit, zero-padded)0000 - 9999
%zTime zone offset from UTC+/-[hh][mm]
%ZTime zone offset from UTC in minutes+/-mmm
%%Literal percent sign%

3. Sample Code

Suppose the collection students contains the following records:

{ "date": "1999-12-11T16:00:00.000Z", "firstName": "Yuanxin", "lastName": "Dong" }
{ "date": "1998-11-10T16:00:00.000Z", "firstName": "Weijia", "lastName": "Wang" }
{ "date": "1997-10-09T16:00:00.000Z", "firstName": "Chengxi", "lastName": "Li" }

Formatting Dates

Below is the formatting of the date field's value into a string in the pattern year-month-day:

// Sample code in the Cloud Function environment
const tcb = require('@cloudbase/node-sdk')
const app = tcb.init({
env: 'xxx'
})

const db = app.database()
const $ = db.command.aggregate

exports.main = async (event, context) => {
const res = await db
.collection('students')
.aggregate()
.project({
_id: 0,
formatDate: $.dateToString({
date: '$date',
format: '%Y-%m-%d'
})
})
.end()
console.log(res.data)
}

The returned result is as follows:

{ "formatDate": "1999-12-11" }
{ "formatDate": "1998-11-10" }
{ "formatDate": "1997-10-09" }

Time Zone Time

Below is an example of formatting the date field value to Shanghai time zone time:

// Sample code in the Cloud Function environment
const tcb = require('@cloudbase/node-sdk')
const app = tcb.init({
env: 'xxx'
})

const db = app.database()
const $ = db.command.aggregate

exports.main = async (event, context) => {
const res = await db
.collection('students')
.aggregate()
.project({
_id: 0,
formatDate: $.dateToString({
date: '$date',
format: '%H:%M:%S',
timezone: 'Asia/Shanghai'
})
})
.end()
console.log(res.data)
}

The returned result is as follows:

{ "formatDate": "00:00:00" }
{ "formatDate": "00:00:00" }
{ "formatDate": "00:00:00" }

Default Value for Missing Cases

When the specified <Date expression> returns null or does not exist, you can set a default value for missing cases:

// Sample code in the Cloud Function environment
const tcb = require('@cloudbase/node-sdk')
const app = tcb.init({
env: 'xxx'
})

const db = app.database()
const $ = db.command.aggregate

exports.main = async (event, context) => {
const res = await db
.collection('students')
.aggregate()
.project({
_id: 0,
formatDate: $.dateToString({
date: '$empty',
onNull: 'null'
})
})
.end()
console.log(res.data)
}

The returned result is as follows:

{ "formatDate": "null" }
{ "formatDate": "null" }
{ "formatDate": "null" }