DigitalConsumer.FixMyGlass/src/helpers/date-helper.js
hiteshkumar87 5a77323820 Update date-helper.js
replacing Math.floor with Math.round
2023-11-15 16:42:37 +05:30

102 lines
4.1 KiB
JavaScript

export function getDateDifferenceInDays(startDate, endDate) {
// Create date objects from the input strings
var date1 = new Date(endDate);
var date2 = new Date(startDate);
// Use the built-in method to get the difference in milliseconds
var difference = date1 - date2;
// Convert milliseconds to days and return the result
return Math.round(difference / (1000 * 3600 * 24));
}
export function get12HourTimeFormat(time) {
// Check correct time format and split into components
time = time.toString().match(/^([01]\d|2[0-3])(:)([0-5]\d)?$/) || [time];
if (time.length > 1) {
// If time format correct
time = time.slice(1); // Remove full string match value
time[5] = +time[0] < 12 ? " AM" : " PM"; // Set AM/PM
time[0] = +time[0] % 12 || 12; // Adjust hours
}
return time.join(""); // return adjusted time or original string
}
export function get12HourTimeMobileFormat(time) {
// Check correct time format and split into components
time = time.toString().match(/^([01]\d|2[0-3])(:)([0-5]\d)?$/) || [time];
if (time.length > 1) {
// If time format correct
const min = time[3];
time = time.slice(1); // Remove full string match value
if (Number(min) == 0) {
time = time.slice(0, 1); // Remove minute value
time[1] = +time[0] < 12 ? " AM" : " PM"; // Set AM/PM
} else {
time[5] = +time[0] < 12 ? " AM" : " PM"; // Set AM/PM
}
time[0] = +time[0] % 12 || 12; // Adjust hours
}
return time.join(""); // return adjusted time or original string
}
export function getDateFormat(date, format) {
if (date instanceof Date !== true) return;
const year = date.getFullYear();
const month = ("0" + (date.getMonth() + 1)).slice(-2);
const day = ("0" + date.getDate()).slice(-2);
const hour = ("0" + date.getHours()).slice(-2);
const minute = ("0" + date.getMinutes()).slice(-2);
const second = ("0" + date.getSeconds()).slice(-2);
return format
.replace("yyyy", year)
.replace("MM", month)
.replace("dd", day)
.replace("hh", hour)
.replace("HH", hour)
.replace("mm", minute)
.replace("ss", second);
}
export function calculateDuration(startDate, endDate) {
if (startDate instanceof Date !== true) return;
if (endDate instanceof Date !== true) return;
return convertMsToTime(endDate - startDate);
}
export function convertMsToTime(milliseconds) {
let seconds = Math.floor(milliseconds / 1000);
let minutes = Math.floor(seconds / 60);
let hours = Math.floor(minutes / 60);
seconds = seconds % 60;
minutes = minutes % 60;
//commenting to get 24 time format
//hours = hours % 24;
return `${padTo2Digits(hours)}${padTo2Digits(minutes)}`;
}
export function padTo2Digits(time) {
// Use the built-in method toString() with a radix of 10 to convert the time value to a decimal string
time = time.toString(10);
// Use the conditional operator to check if the length of the string is less than 2
return time.length < 2
? // If yes, prepend a '0' to the string and return it
"0" + time
: // If no, return the original string
time;
}
export function combineDateAndTime(date, time) {
// Use the Date.parse() method to convert the date and time strings to a numeric value
var timestamp = Date.parse(date + "T" + time);
// Use the new Date() constructor to create a new date object from the numeric value
var newDate = new Date(timestamp);
// Return the new date object
return newDate;
}
export function addMinutes(date, minutes) {
return new Date(date.getTime() + minutes * 60000);
}
export function shortTimeString(date) {
// Use a ternary operator to check if the input is a valid date object
return date instanceof Date
? // Use the built-in method toLocaleTimeString() to get the short time string in the current locale
date.toLocaleTimeString("en-us", { hour: "numeric", minute: "numeric", hour12: true })
: // Return undefined if the input is not a valid date object
undefined;
}