We have created an extension in which order time will be stored in the Sales Order (SO). Based on the current time and the order time, we will provide an option for customers to edit an order. They can add, remove, and update items. This edit option is available on the My Account page. The edit option will be available for orders that are in the pending approval state and for orders placed within 1 hour.
For this we have created a custom field in which we store GMT 0, To store the current time in GMT 0 (Greenwich Mean Time), you can simply create a Date object and use the toISOString() method, which returns the date in UTC (GMT 0) format.
Sample to check current time is within one hour of Ordered time
// Function to check if the getCurrentTime is within 1 hour of createdTime
function isWithinOneHour(createdTime, getCurrentTime) {
// Parse the input strings into Date objects
const createdDate = new Date('20' + createdTime.replace(///g, '-') + 'Z');
const getCurrentDate = new Date('20' + getCurrentTime.replace(///g, '-') + 'Z');
// Calculate the difference in milliseconds between the two times
const timeDiff = Math.abs(getCurrentDate - createdDate);
// Define the duration of 1 hour in milliseconds
const oneHourInMillis = 60 * 60 * 1000;
// Check if the time difference is within 1 hour
return timeDiff <= oneHourInMillis;
}
// Test case
const createdTime = '05/09/24 12:43:12';
const getCurrentTime = '05/09/24 12:46:03';
console.log(isWithinOneHour(createdTime, getCurrentTime)); // Output: true