this depends on a bit on the format of what you're converting but made this you can use, maybe it will help?
import moment from "moment";
import "moment-timezone";
//****************************
// UNCOMMENT ALL LINES BELOW HERE TO ALSO TEST POSTGRESQL TIME
//****************************
// const { Client } = require("pg");
// const pgclient = new Client({
// connectionString: process.env.PG_CONNECTION_STRING,
// });
const envFilePath =
process.env.NODE_ENV === "development" ? "./.dev.env" : "./.env";
require("dotenv").config({ path: envFilePath });
// async function connectPGClient() {
// try {
// await pgclient.connect();
// console.info("CONNECTED: PG Client");
// } catch (err) {
// console.error("Connection error", err.stack);
// }
// }
// async function getPGTime() {
// try {
// const pg_now = "SELECT now()";
// const pg_now_tz = "SELECT extract(epoch FROM now())";
// const now_ret = await pgclient.query(pg_now);
// const now_tz_ret = await pgclient.query(pg_now_tz);
// const now_obj = now_ret.rows;
// const now_tz_obj = now_tz_ret.rows;
// return {
// now: {
// value: now_obj,
// comment: "PG timestamp without timezone",
// },
// now_tz: {
// value: now_tz_obj,
// comment: "PG timestamp with timezone",
// },
// };
// } catch (err) {
// console.error("Query Error:", err.stack);
// throw err;
// }
// }
function getMomentTime(time_string, format, timezone) {
const moment_ts = moment(time_string, format, timezone).format();
const moment_tz = moment(time_string, format).tz(timezone).format();
return {
moment_ts: {
value: moment_ts,
comment:
"moment converted time_string to browsers timezone THEN to " + timezone,
},
moment_tz: {
value: moment_tz,
comment: "moment converted time_string to " + timezone,
},
};
}
function getJSDateTime(time_string = "2024-8-23 13:00:00", timezone = "UTC") {
const date = new Date(time_string);
if (isNaN(date.getTime())) {
throw new Error("Invalid time value");
}
const date_iso = date.toISOString();
const date_value = date.valueOf();
const date_locale = date.toLocaleString("en-US", { timeZone: timezone });
return {
date_iso: {
value: date_iso,
comment: "Date() using ISO String",
},
date_value: {
value: date_value,
comment: "Date() using valueOf",
},
date_locale: {
value: date_locale,
comment: "Date() using locale String",
},
};
}
async function getTimeComparison(
time_string = "2024-8-23 13:00:00",
format = "YYYY/MM/DD HH:mm:ss",
timezone = "UTC"
) {
// const pg_time = await getPGTime();
const moment_time = getMomentTime(time_string, format, timezone);
const date_time = getJSDateTime();
return {
// postgresql: pg_time,
moment: moment_time,
js_date: date_time,
};
}
async function main() {
// await connectPGClient();
const my_timestamp = "2024-8-23 13:00:00";
const format = "YYYY/MM/DD HH:mm:ss";
const timezone = "UTC";
const timestamps = await getTimeComparison(my_timestamp, format, timezone);
console.log("Input:", {
timestamp: my_timestamp,
format: format,
timezone: timezone,
});
console.log("Output:", timestamps);
}
main();