The $dateToString operator formats a BSON Date into a text string inside MongoDB aggregation pipelines. Use it for report labels, API responses, CSV exports, or any output where dates must appear in a specific human-readable layout.
01
Format Dates
Date → string.
02
Syntax
date, format.
03
Specifiers
%Y, %m, %d, etc.
04
Timezone
Local display time.
05
Use Cases
Reports, exports.
06
vs $dateFromString
Format vs parse.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $dateToString operator takes a BSON Date and a format pattern and returns a string. For example, formatting ISODate("2024-06-15T14:30:00Z") with format: "%Y-%m-%d" produces "2024-06-15".
💡
Beginner Tip
$dateToString returns a string, not a Date. Both date and format are required. Use timezone when the formatted string should reflect local time rather than UTC.
Foundation
📝 Syntax
The $dateToString operator takes an object with a required date and format:
The timezone field shifts how the date is interpreted before formatting. %Z appends the offset. Essential when the same UTC instant should read differently per region.
Example 4 — Handle Missing Dates with onNull
Return a placeholder when optional date fields are null:
onNull avoids pipeline errors and gives user-friendly output when dates are optional. Keep the raw lastLogin field for logic; use the formatted string only for display.
Applications
🚀 Use Cases
Report and dashboard labels — show dates in a consistent readable format.
CSV and JSON exports — serialize dates as strings for downstream tools.
Email and notification templates — embed formatted timestamps in message bodies.
Grouping keys — bucket documents by formatted date strings in $group (prefer BSON dates for performance when possible).
🧠 How $dateToString Works
1
MongoDB resolves the input date
date is evaluated per document — a field, $$NOW, or literal ISODate.
Input
2
$dateToString applies the format
MongoDB maps the date through format specifiers, optionally using timezone.
Format
3
A string is returned
If date is null, onNull provides a fallback; otherwise the formatted text is output.
Output
=
📅
Human-readable date text
Use in exports, UI labels, or alongside $dateFromString for round-trip string conversion.
Wrap Up
Conclusion
The $dateToString operator is the standard way to turn BSON dates into formatted text in MongoDB aggregation pipelines. With flexible format specifiers and optional timezone support, it replaces manual string concatenation for display output.
Pair it with $dateFromString when you need to parse strings back into dates, and keep BSON Date fields in your schema for indexing and range queries. Use formatted strings only where human readability matters.
Keep BSON dates in the database; format at query time for display
Use timezone when users expect local calendar dates
Match format to your export or API contract
Use onNull for optional date fields in reports
Document your format strings for team consistency
❌ Don’t
Store dates only as strings if you need range queries or sorting
Confuse $dateToString with $dateFromString
Forget timezone when displaying local business hours
Group on formatted strings when numeric parts from $dateToParts suffice
Assume string dates sort correctly without zero-padded %m and %d
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $dateToString
Use these points when formatting dates as strings in MongoDB.
5
Core concepts
📅01
Format Dates
Date → string.
Purpose
📝02
Two Required
date, format.
Syntax
🛠03
String Output
Not a Date type.
Output
🌐04
Timezone
Local display time.
Option
⚠05
Not $dateFromString
Format vs parse.
Important
❓ Frequently Asked Questions
$dateToString converts a BSON Date into a formatted text string inside aggregation pipelines. Use it for human-readable output, CSV exports, labels in reports, or storing display-friendly date text.
{ $dateToString: { date: <date>, format: <string>, timezone: <string>, onNull: <string> } }. date and format are required. The format string uses specifiers like %Y, %m, and %d.
$dateToString formats a BSON Date into a string. $dateFromString parses a string into a BSON Date. They are inverse operations for text and date conversion.
Common specifiers include %Y (year), %m (month), %d (day), %H (hour), %M (minute), %S (second), and %L (millisecond). Combine them in the format string, e.g. "%Y-%m-%d" for 2024-06-15.
$dateToString is available in MongoDB 3.6 and later in aggregation pipelines and update pipelines that use aggregation expressions.