I learned from this forum in the past to generate a current date and time using executeScript_Sandbox and I managed to input exact current time and date with javascript. However, I could never figure out how to insert leading zero’s into it.
For reference this is how sheets likes it:
2023/06/04 14:13:40
Best I can make work with UI vision is: 2023/6/4 13:53:6
I remember I spent some good hours a while ago and could never make it work, I was wondering maybe someone could help me fix the leading zero’s issue? Thank you!
Here’s the script I use to achieve my current best option:
Thank you so much for your input, this didn’t work out of the box, potantially my own fault? as I tried to replace each \n by hand but would still get an error: Expecting Unicode escape sequence \uXXXX (7:6)
I tried to troubleshoot some bits and seems by removing \t, adding ; at the end and removing all of the \ from 4th line done it for me so here’s the final code that does work perfectly for me! Thank you so much, without this I could not have figured it out
var d = new Date(new Date().getTime());
var m = ((d.getMonth() + 1) < 10) ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1);
var dy = ((d.getDate()) < 10) ? '0' + (d.getDate()) : (d.getDate());
m = dy + "." + m + "." + d.getFullYear();
return m;
Edit2: I did realise it only returns the date, not time. What did I get wrong?
Edit3: Okay ChatGPT did it for me haha. Here’s the final code, thanks again!
var d = new Date();
var m = ((d.getMonth() + 1) < 10) ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1);
var dy = ((d.getDate()) < 10) ? '0' + (d.getDate()) : (d.getDate());
var hours = (d.getHours() < 10) ? '0' + d.getHours() : d.getHours();
var minutes = (d.getMinutes() < 10) ? '0' + d.getMinutes() : d.getMinutes();
var seconds = (d.getSeconds() < 10) ? '0' + d.getSeconds() : d.getSeconds();
var formattedDateTime = dy + "/" + m + "/" + d.getFullYear() + " " + hours + ":" + minutes + ":" + seconds;
return formattedDateTime;