Hi @henna.s
Is there any update on this? date-fns-tz is not working. Getting invalid date with utcToZonedTime
const { format, getMonth, getYear, getQuarter, getWeek } = require(‘date-fns’);
const { zonedTimeToUtc, utcToZonedTime } = require(‘date-fns-tz’);
const { omit, uniqueId } = require(‘lodash’);
function getDateDetailsWithTimezone(utcDate, timeZone) {
// Convert UTC date to the specified timezone
//const zonedDate = new Date(utcDate.toLocaleString(‘en-US’, { timeZone }));
const zonedDate = utcToZonedTime(new Date(utcDate), timeZone);
const date = format(zonedDate, ‘yyyy-MM-dd’);
const startOfDay = new Date(zonedDate);
startOfDay.setHours(0, 0, 0, 0);
const datetimestamp = startOfDay.toISOString();
const month = getMonth(zonedDate) + 1;
const year = getYear(zonedDate);
const quarter = getQuarter(zonedDate);
const halfYearly = month <= 6 ? 1 : 2;
const week = getWeek(zonedDate);
return {
date,
datetimestamp,
week,
month,
year,
quarter,
halfYearly
};
}
exports = async function(arg){
// This default function will get a value and find a document in MongoDB
// To see plenty more examples of what you can do with functions see:
// https://www.mongodb.com/docs/atlas/app-services/functions/
// Find the name of the MongoDB service you want to use (see “Linked Data Sources” tab)
// To call other named functions:
console.log(arg)
// var result = context.functions.execute(“function_name”, arg1, arg2);
const result = getDateDetailsWithTimezone(“2025-12-31T18:30:00Z”, “Asia/Kolkata”)
return JSON.stringify(result);
};