Java Script
Online Debuggers
Node JS
- Node Cache - https://www.npmjs.com/package/node-cache
Access SQL
Web App - SQL
AJAX - JSON Array - Update a table
JSON PATH
DateTime
const message = "hello";
let date = new Date();
date.setSeconds(date.getSeconds() + 600);
year = date.getFullYear();
month = date.getMonth();
time = date.toISOString()
// Update header text
document.querySelector('#header').innerHTML = message
// Log to console
console.log(message)
console.log(date)
console.log(time)
console.log(year)
console.log(month)
---CONSOLE output ---
hello
Tue Jul 25 2023 11:09:20 GMT+0100 (Irish Standard Time)
2023-07-25T10:09:20.739Z
2023
6
let date = new Date();
let currentTime = date.setSeconds(date.getSeconds());//
let currentTimeIso = date.toISOString()
console.log("currentTime:",currentTimeIso)
function getRoundedDate(minutes, d=new Date()) {
let ms = 1000 * 60 * minutes; // convert minutes to ms
// Lets add 60 seconds (60000ms) onto current time and THEN round UP to nearest 5 mins.
let roundedDate = new Date(Math.ceil((d.getTime() + 60000) / ms) * ms);
return roundedDate}
let startTime = getRoundedDate(5, new Date());
let startTimeIso = startTime.toISOString()
console.log("startTime:",startTime)
console.log("startTimeIso:",startTimeIso)
---- console output ----
currentTime:2023-07-26T08:25:48.103Z
startTime:Wed Jul 26 2023 09:30:00 GMT+0100 (Irish Standard Time)
startTimeIso:2023-07-26T08:30:00.000Z
function addMinutes(date, minutes) {
return new Date(date.getTime() + minutes*60000);
}
let roundUpMin = 5;
let meetDurationMin = 60;
let ms = 1000 * 60 * roundUpMin; // e.g. convert 5 minutes to ms
let d = new Date();
//Note before we round to nearest 5 mins we also add on one more minute, so 12:49 will round to 12:55 and not 12:50.
let myStartTime = new Date(Math.ceil((d.getTime() + 60000) / ms) * ms);
let myEndTime = new Date(addMinutes(myStartTime, meetDurationMin));
startTime = myStartTime.toISOString();
endTime = myEndTime.toISOString();
1;
Convert an Array into JSON with a specific object format
const fruitsArray = ["apple", "orange", "banana"];
// Map the array to an array of objects with the desired format
const fruitsObjects = fruitsArray.map((fruit) => ({ fruit }));
// Convert the array of objects to a JSON string
const jsonString = JSON.stringify(fruitsObjects);
console.log(jsonString);
---- console ---
[{"fruit":"apple"},{"fruit":"orange"},{"fruit":"banana"}]
Round up time to nearest hour
function roundUpToNearestHour(datetime) {
// Clone the datetime to avoid modifying the original value
const roundedDatetime = new Date(datetime);
// Get the minutes part of the datetime
const minutes = roundedDatetime.getMinutes();
// If minutes is greater than 0, add 1 hour to round up
if (minutes > 0) {
roundedDatetime.setHours(roundedDatetime.getHours() + 1);
}
// Set minutes and seconds to 0 to get the rounded hour
roundedDatetime.setMinutes(0);
roundedDatetime.setSeconds(0);
return roundedDatetime;
}
// Example usage:
const currentDatetime = new Date(); // Get the current datetime
const roundedDatetime = roundUpToNearestHour(currentDatetime);
console.log("Original datetime:", currentDatetime);
console.log("Rounded datetime:", roundedDatetime);
Convert DateTime to UTC format from a specific Timezone
function convertToUTC(datetime, timeZone) {
// Create a formatter for the specific timezone
const formatter = new Intl.DateTimeFormat('en-US', {
timeZone,
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
hour12: false,
});
// Format the datetime in the specific timezone
const parts = formatter.formatToParts(datetime);
// Extract date and time components
const year = parseInt(parts.find(part => part.type === 'year').value, 10);
const month = parseInt(parts.find(part => part.type === 'month').value, 10) - 1;
const day = parseInt(parts.find(part => part.type === 'day').value, 10);
const hour = parseInt(parts.find(part => part.type === 'hour').value, 10);
const minute = parseInt(parts.find(part => part.type === 'minute').value, 10);
const second = parseInt(parts.find(part => part.type === 'second').value, 10);
// Create a new Date object in the UTC timezone
return new Date(Date.UTC(year, month, day, hour, minute, second));
}
// Example usage:
const datetimeString = '2023-07-27T12:34:56';
const timeZone = 'America/New_York';
const datetime = new Date(datetimeString);
const utcDatetime = convertToUTC(datetime, timeZone);
console.log(utcDatetime.toISOString()); // UTC formatted datetime
function convertToUTC(datetimeString, timeZone) {
const datetime = new Date(datetimeString);
const options = {
timeZone,
timeZoneName: 'short',
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
};
const utcDatetimeString = datetime.toLocaleString('en-US', options);
return new Date(utcDatetimeString).toISOString();
}
// Example usage:
const datetimeString = '2023-07-27T11:50';
const timeZone = 'America/New_York';
const utcDatetime = convertToUTC(datetimeString, timeZone);
console.log(utcDatetime); // Output: '2023-07-27T10:50:00.000Z'
Convert time into Local Time (this does work!)
Example1 - Time Now
date = new Date().toLocaleString('en-GB', { timeZone: 'Europe/London' })
console.log(date);
Example2 - a specific Time
new Date("2020-09-17T17:15:00.000Z").toLocaleString('en-GB', { timeZone: 'Europe/London' })
Remove Z from end of ISO Time
let date = new Date().toISOString().split('.').shift() ;
console.log(date);
LocalTime Issues
Some JavaScript implementations do not support LocalTime Timezones - e.g. Webex Connect!!! AHHHHH!
Here is a manual way for next 10 years to set the time correctly (setting UTC to be the Irish / British Summer time)
date = new Date();
//date = new Date('2024-05-10T05:00:00.000Z');
year = date.getFullYear();
ist = date; //Irish Standard Time (note we do need to remove the Z later!)
startSummerTime2023 = Date.parse('2023-03-26T01:00:00.000Z');
endSummerTime2023 = Date.parse('2023-10-29T01:00:00.000Z');
startSummerTime2024 = Date.parse('2024-03-31T01:00:00.000Z');
endSummerTime2024 = Date.parse('2024-10-27T01:00:00.000Z');
startSummerTime2025 = Date.parse('2025-03-30T01:00:00.000Z');
endSummerTime2025 = Date.parse('2025-10-26T01:00:00.000Z');
startSummerTime2026 = Date.parse('2026-03-29T01:00:00.000Z');
endSummerTime2026 = Date.parse('2026-10-25T01:00:00.000Z');
startSummerTime2027 = Date.parse('2027-03-28T01:00:00.000Z');
endSummerTime2027 = Date.parse('2027-10-31T01:00:00.000Z');
startSummerTime2028 = Date.parse('2028-03-26T01:00:00.000Z');
endSummerTime2028 = Date.parse('2028-10-29T01:00:00.000Z');
startSummerTime2029 = Date.parse('2029-03-25T01:00:00.000Z');
endSummerTime2029 = Date.parse('2029-10-28T01:00:00.000Z');
startSummerTime2030 = Date.parse('2030-03-31T01:00:00.000Z');
endSummerTime2030 = Date.parse('2030-10-27T01:00:00.000Z');
startSummerTime2031 = Date.parse('2031-03-30T01:00:00.000Z');
endSummerTime2031 = Date.parse('2031-10-26T01:00:00.000Z');
startSummerTime2032 = Date.parse('2032-03-28T01:00:00.000Z');
endSummerTime2032 = Date.parse('2032-10-31T01:00:00.000Z');
startSummerTime2033 = Date.parse('2033-03-27T01:00:00.000Z');
endSummerTime2033 = Date.parse('2033-10-30T01:00:00.000Z');
startSummerTime2034 = Date.parse('2034-03-26T01:00:00.000Z');
endSummerTime2034 = Date.parse('2034-10-29T01:00:00.000Z');
//Check if current date in UTC is in Summer Time DateTime Range
if ( (date > startSummerTime2023 && date < endSummerTime2023)
|| (date > startSummerTime2024 && date < endSummerTime2024)
|| (date > startSummerTime2025 && date < endSummerTime2025)
|| (date > startSummerTime2026 && date < endSummerTime2026)
|| (date > startSummerTime2027 && date < endSummerTime2027)
|| (date > startSummerTime2028 && date < endSummerTime2028)
|| (date > startSummerTime2029 && date < endSummerTime2029)
|| (date > startSummerTime2030 && date < endSummerTime2030)
|| (date > startSummerTime2031 && date < endSummerTime2031)
|| (date > startSummerTime2032 && date < endSummerTime2032)
|| (date > startSummerTime2033 && date < endSummerTime2033)
|| (date > startSummerTime2034 && date < endSummerTime2034) )
{
ist.setHours(date.getHours()+1);
ist = date.toISOString();
console.log(ist)
//1;
} else if ((year >= 2023) && (year < 2035){
//Do not change time - as its time equal to UTC
ist = date.toISOString();
console.log(ist)
//2;
} else {
//error - contact your admin
console.log('error - outside daterange - contact your admin');
}
Convert ISO Time to IST Format
date = '2023-03-26T01:23:45.000Z'; formatdate = date.slice(8,10) + '/' + date.slice(5,7) + '/'+date.slice(0,4) formattime = date.slice(11,13) + ':' + date.slice(14,16); console.log(formatdate); console.log(formattime);
Check if Current Date is within 30 dates of manually enters date time in IST format
startTime = '2023-08-04T15:59:00.000Z';
startTimeDate = Date.parse(startTime);
startTimeDate = new Date(startTimeDate);
//We are going to check if the entered time is valid.
//A valid time is in the future from now and within the next 30 days.
//since the 'startTime' we have is in UTC format but user enters it in Irish Standard Time,
//We therefore need to get the current time and convert this to IST.
// This set the variable 'ist' to be current time in IST but says its UTC.
date = new Date(); //Current Time
ist = date;
year = date.getFullYear();
let istPlus30Days = new Date(ist);
startSummerTime2023 = Date.parse('2023-03-26T01:00:00.000Z');
endSummerTime2023 = Date.parse('2023-10-29T01:00:00.000Z');
startSummerTime2024 = Date.parse('2024-03-31T01:00:00.000Z');
endSummerTime2024 = Date.parse('2024-10-27T01:00:00.000Z');
startSummerTime2025 = Date.parse('2025-03-30T01:00:00.000Z');
endSummerTime2025 = Date.parse('2025-10-26T01:00:00.000Z');
startSummerTime2026 = Date.parse('2026-03-29T01:00:00.000Z');
endSummerTime2026 = Date.parse('2026-10-25T01:00:00.000Z');
startSummerTime2027 = Date.parse('2027-03-28T01:00:00.000Z');
endSummerTime2027 = Date.parse('2027-10-31T01:00:00.000Z');
startSummerTime2028 = Date.parse('2028-03-26T01:00:00.000Z');
endSummerTime2028 = Date.parse('2028-10-29T01:00:00.000Z');
startSummerTime2029 = Date.parse('2029-03-25T01:00:00.000Z');
endSummerTime2029 = Date.parse('2029-10-28T01:00:00.000Z');
startSummerTime2030 = Date.parse('2030-03-31T01:00:00.000Z');
endSummerTime2030 = Date.parse('2030-10-27T01:00:00.000Z');
startSummerTime2031 = Date.parse('2031-03-30T01:00:00.000Z');
endSummerTime2031 = Date.parse('2031-10-26T01:00:00.000Z');
startSummerTime2032 = Date.parse('2032-03-28T01:00:00.000Z');
endSummerTime2032 = Date.parse('2032-10-31T01:00:00.000Z');
startSummerTime2033 = Date.parse('2033-03-27T01:00:00.000Z');
endSummerTime2033 = Date.parse('2033-10-30T01:00:00.000Z');
startSummerTime2034 = Date.parse('2034-03-26T01:00:00.000Z');
endSummerTime2034 = Date.parse('2034-10-29T01:00:00.000Z');
//Check if current date in UTC is in Summer Time DateTime Range and if so add an hour to 'ist'
if ((date > startSummerTime2023 && date < endSummerTime2023) || (date > startSummerTime2024 && date < endSummerTime2024) || (date > startSummerTime2025 && date < endSummerTime2025) || (date > startSummerTime2026 && date < endSummerTime2026) || (date > startSummerTime2027 && date < endSummerTime2027) || (date > startSummerTime2028 && date < endSummerTime2028) || (date > startSummerTime2029 && date < endSummerTime2029) || (date > startSummerTime2030 && date < endSummerTime2030) || (date > startSummerTime2031 && date < endSummerTime2031) || (date > startSummerTime2032 && date < endSummerTime2032) || (date > startSummerTime2033 && date < endSummerTime2033) || (date > startSummerTime2034 && date < endSummerTime2034)) {
ist.setHours(date.getHours()+1);
} else if ((year >= 2023) && (year < 2035)) {
//Do not change time - as its time equal to UTC
} else {
3;
}
istPlus30Days.setHours(ist.getHours()+(24*30)); // Add 30 days to ist time
if ((startTimeDate > ist) && (startTimeDate < istPlus30Days)){
console.log("Valid");
}else{
console.log("InValid");
}