Changing the date format to North American date format

Updated on 20-October-2016 at 10:16 AM

Business Catalyst End of life announcement - find out more details.

Events dates

When using the Events module, you can use {tag_counter} to generate the item ID and to access the date. Then, you can use some JavaScript to re-format the date.

In this example, this tag {tag_eventfromdate} is reformatted, but you can use the same method for any date tag. Here is the HTML code you need to use in your announcement layout. Note that the important part is

id="date{tag_counter}"

You'll add this code in the List layout. In the Detailed layout, just change the id from id="date{tag_counter} to something else, such as id="dateid".

<span id="date{tag_counter}">{tag_eventfromdate}</span>

The JavaScript below converts the default date format (23-Apr-2009) into a North American date format: Apr/23/2009.

<script type="text/javascript">  
var originalDate = document.getElementById(&#39;date{tag_counter}&#39;).innerHTML;  
var dateBits = originalDate.split("-");  
document.write(dateBits[1] + "/" + dateBits[0] + "/" + dateBits[2]);  
</script>  

Note: You can feed the date directly into the var originalDate like this:

var originalDate = "{tag_eventfromdate}";

Media Downloads dates

Similar to Events, you can update Media Downloads items by using {tag_counter} to generate the item ID and access the date. Then use some JavaScript to re-format the date. In the example below, the tag {tag_expirydate} is reformatted. However, keep in mind that you can use the same method for any date tag. Here is the HTML code you need to use in your announcement layout. Note that the important part of the code is:

id="date{tag_counter}"

Add this code to the List layout. In the Detailed layout, simply change the id from id="date{tag_counter} to something else, such as id="dateid".

<span id="date{tag_counter}">{tag_expirydate}</span>

The following JavaScript code will take the date formatted like this: 23-Apr-2009 and convert it to a North American date format, like this: Apr/23/2009.

<script type="text/javascript">  
var originalDate = document.getElementById(&#39;date{tag_counter}&#39;).innerHTML;  
var dateBits = originalDate.split("-");  
document.write(dateBits[1] + "/" + dateBits[0] + "/" + dateBits[2]);  
</script>

Note: You can feed the date directly into var originalDate, like this

var originalDate = "{tag_expirydate}";