Skip to main content
deleted 2 characters in body
Source Link
takatama
  • 420
  • 4
  • 6

You can make it shorter:

<input type="datetime-local" id="cal"> 
function toLocaleIsoStringtoLocalISOString(date) { const localDate = new Date(date - date.getTimezoneOffset() * 60000); //offset in milliseconds. Credit https://stackoverflow.com/questions/10830357/javascript-toisostring-ignores-timezone-offset // Optionally remove second/millisecond if needed localDate.setSeconds(null); localDate.setMilliseconds(null); return localDate.toISOString().slice(0, -1); } window.addEventListener("load", () => { document.getElementById("cal").value = toLocaleIsoStringtoLocalISOString(new Date()); }); 

You can make it shorter:

<input type="datetime-local" id="cal"> 
function toLocaleIsoString(date) { const localDate = new Date(date - date.getTimezoneOffset() * 60000); //offset in milliseconds. Credit https://stackoverflow.com/questions/10830357/javascript-toisostring-ignores-timezone-offset // Optionally remove second/millisecond if needed localDate.setSeconds(null); localDate.setMilliseconds(null); return localDate.toISOString().slice(0, -1); } window.addEventListener("load", () => { document.getElementById("cal").value = toLocaleIsoString(new Date()); }); 

You can make it shorter:

<input type="datetime-local" id="cal"> 
function toLocalISOString(date) { const localDate = new Date(date - date.getTimezoneOffset() * 60000); //offset in milliseconds. Credit https://stackoverflow.com/questions/10830357/javascript-toisostring-ignores-timezone-offset // Optionally remove second/millisecond if needed localDate.setSeconds(null); localDate.setMilliseconds(null); return localDate.toISOString().slice(0, -1); } window.addEventListener("load", () => { document.getElementById("cal").value = toLocalISOString(new Date()); }); 
Fixing to use locale. Date and datetime-local inputs expect the value to be in local time, so I had to offset the date to get it to display the correct current time. Tested here https://jsfiddle.net/duto_guerra/L4kg8ped/
Source Link

You can make it shorter:

<input type="datetime-local" id="cal"> 
window.addEventListener('load',function toLocaleIsoString(date) => { varconst nowlocalDate = new Date(); now.setMinutes(now.getMinutes()date - nowdate.getTimezoneOffset());  /* remove60000); second/millisecond if needed -/offset creditin refmilliseconds. Credit https://stackoverflow.com/questions/2446851810830357/html5-input-datetime-local-default-value-ofjavascript-todaytoisostring-andignores-currenttimezone-time#comment112871765_60884408offset  * /  / Optionally now.setMilliseconds(null)remove second/millisecond if needed nowlocalDate.setSeconds(null)  ; documentlocalDate.getElementByIdsetMilliseconds('cal'null).value;  = nowreturn localDate.toISOString().slice(0, -1); } window.addEventListener("load", () => { document.getElementById("cal").value = toLocaleIsoString(new Date()); }); 

You can make it shorter:

<input type="datetime-local" id="cal"> 
window.addEventListener('load', () => { var now = new Date(); now.setMinutes(now.getMinutes() - now.getTimezoneOffset());  /* remove second/millisecond if needed - credit ref. https://stackoverflow.com/questions/24468518/html5-input-datetime-local-default-value-of-today-and-current-time#comment112871765_60884408 */   now.setMilliseconds(null) now.setSeconds(null)   document.getElementById('cal').value = now.toISOString().slice(0, -1); }); 

You can make it shorter:

<input type="datetime-local" id="cal"> 
function toLocaleIsoString(date) { const localDate = new Date(date - date.getTimezoneOffset() * 60000); //offset in milliseconds. Credit https://stackoverflow.com/questions/10830357/javascript-toisostring-ignores-timezone-offset   // Optionally remove second/millisecond if needed localDate.setSeconds(null); localDate.setMilliseconds(null);  return localDate.toISOString().slice(0, -1); } window.addEventListener("load", () => { document.getElementById("cal").value = toLocaleIsoString(new Date()); }); 
added 254 characters in body
Source Link
Nam G VU
  • 35.8k
  • 79
  • 248
  • 399

You can make it shorter:

<input type="datetime-local" id="cal"> 
window.addEventListener('load', () => { constvar now = new Date(); now.setMinutes(now.getMinutes() - now.getTimezoneOffset()); /* remove second/millisecond if needed - credit ref. https://stackoverflow.com/questions/24468518/html5-input-datetime-local-default-value-of-today-and-current-time#comment112871765_60884408 */ now.setMilliseconds(null) now.setSeconds(null)  document.getElementById('cal').value = now.toISOString().slice(0, -1); }); 

You can make it shorter:

<input type="datetime-local" id="cal"> 
window.addEventListener('load', () => { const now = new Date(); now.setMinutes(now.getMinutes() - now.getTimezoneOffset()); document.getElementById('cal').value = now.toISOString().slice(0, -1); }); 

You can make it shorter:

<input type="datetime-local" id="cal"> 
window.addEventListener('load', () => { var now = new Date(); now.setMinutes(now.getMinutes() - now.getTimezoneOffset()); /* remove second/millisecond if needed - credit ref. https://stackoverflow.com/questions/24468518/html5-input-datetime-local-default-value-of-today-and-current-time#comment112871765_60884408 */ now.setMilliseconds(null) now.setSeconds(null)  document.getElementById('cal').value = now.toISOString().slice(0, -1); }); 
Source Link
takatama
  • 420
  • 4
  • 6
Loading