Moment.js is a fine library, but it is heavy and its maintainers consider it a legacy project. See this page for an explanation and a list of alternatives that the moment.js team suggests.
One thing to look into in native JavaScript is the Intl.DateTimeFormat object, which allows you to format a Date in a highly customizable, locale-based format. You can choose locale formats that are close to what you're looking for and manipulate them to get the final desired result. For example,
const date = new Date(Date.now());
const formatOptions = {
timeZone: 'Asia/Jakarta',
dateStyle: 'short',
hour12: false,
timeStyle: 'medium'
};
console.log(new Intl.DateTimeFormat('en-CA',formatOptions).format(date).split(',').join(''));
// example output: "2022-07-24 04:29:31"
This relies on the fact that Canadians format their dates YYYY-MM-DD, for example. Additionally, the split
and join
are necessary because the formatter initially gives you "2022-07-24, 04:29:31"
Finally, if you have control over how these are written to the mysql database, you can use the STR_TO_DATE function to convert any date string from a known format into a MySQL DATETIME value.