Date Time
Functions
mysql_datetime(date: Date | string): string
This function takes a Date
or string
as input and formats it into MySQL's DATETIME
format (YYYY-MM-DD HH:mm:ss
).
Parameters
date
: ADate
object or a string that can be parsed as a date.- Example:
2024-11-24T14:30:00Z
, ornew Date()
.
- Example:
Returns
- A string representing the date in MySQL
DATETIME
format:YYYY-MM-DD HH:mm:ss
.
Example Usage
import { mysql_datetime } from 'agile-query';
const date1 = mysql_datetime(new Date());
console.log(date1);
// Output: "2024-11-24 14:30:00"
const date2 = mysql_datetime('2024-11-24T14:30:00Z');
console.log(date2);
// Output: "2024-11-24 14:30:00"
mysql_date(date: Date | string): string
This function takes a Date
or string
as input and formats it into MySQL's DATE
format (YYYY-MM-DD
).
Parameters
date
: ADate
object or a string that can be parsed as a date.- Example:
2024-11-24T14:30:00Z
, ornew Date()
.
- Example:
Returns
- A string representing the date in MySQL
DATE
format:YYYY-MM-DD
.
Example Usage
import { mysql_date } from 'agile-query';
const date1 = mysql_date(new Date());
console.log(date1);
// Output: "2024-11-24"
const date2 = mysql_date('2024-11-24T14:30:00Z');
console.log(date2);
// Output: "2024-11-24"
Notes
- The
mysql_datetime
function ensures the date is always formatted in a consistent way, even for invalid input strings. - The
mysql_date
function provides a simpler date format, useful when only the date part (without time) is needed.