JavaScript Date Formatting HELP!

  • 4 January 2022
  • 0 replies
  • 10 views

Hello,

We have this script running on an UnBounce landing page, that is pushing a timestamp into a hidden field called ‘Conversion_Time’, it works fine, but the date needs to be formatted like, MM/DD/YYYY HH:MM:SS (01/04/2022 10:10:54). I found it here in the community. It works great, but the date is not formatted properly for Google Ad reporting.

The date formats incorrectly like this currently, YYYY/MM/DD HH:MM:SS (2022/01/04 10:10:54). Can anyone help? Please note the TIME is fine, the date just needs to have the year at the end.

See the script below. Thanks SO much, I really appreciate any help.

<script>
// Change the word Date_Submitted in the following line to the name of your hidden field label.
// It has to match exact label, including capitals, underscore, etc...
var inpElm = document.getElementById('Conversion_Time');                                     
var d = new Date();
var pD = d;
var tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
var localISOTime = (new Date(Date.now() - tzoffset)).toISOString().slice(0, -1);  
window.setTimeout(sD, 5000);
window.setInterval(sD, 30000);
function sD(){
dt = new Date();
inpElm.value = dt.toLocaleDateString().concat(" ", dt.toLocaleTimeString());
inpElm.value = localISOTime.slice(0, 19).replace(/-/g, "/").replace("T", " ");  
}
</script>

0 replies

Be the first to reply!

Reply