Rich-text Reply

Using Optimizely to convert date format by country

dansullivan 11-17-14

Using Optimizely to convert date format by country

 I have a need to do country – level customization for various bits of site content like dates.


I would like to convert a date in this format: 22-06-2014

To this format: 06-22-2014


For countries like the US that put month first.  With dates like June 22 it's not an issue for June 8th might be a problem.


Can this be done?


Thanks in advance,



JohnH 11-17-14

Re: Using Optimizely to convert date format by country

[ Edited ]

@dansullivan - just to make sure I am understanding your question, the date exists on your site itself as 22-06-2014. Correct? Can you share the URL where this exists by any chance?


You should be able to use some RegEx or some JavaScript to split the element at the "-" and then re-arrange as needed. I would assume it's just string manipulation, but would need to see how the date actually shows up on your page to fully confirm. 


Here's a sample where the intial date 11-22-06 is transformed to 22-11-06 (the new_date variable). Feel free to swap around as needed. 

var str = "11-22-06";
var month = str.split("-")[0]
var day = str.split("-")[1]
var year = str.split("-")[2]
var new_date = day + "-" + month + "-" + year


Level 2
JDahlinANF 11-18-14

Re: Using Optimizely to convert date format by country

If you need it to be dynamic based on the users locale preferences, you could try something like this:


1- create an Audience that limit the experiment to browsers that support "toLocaleDateString"


typeof Date.prototype.toLocaleDateString !== 'undefined'

 2- presuming that your date is in an element such as <span id="shipDate">, fetch the date, modify it, and replace it, like this:

try {
  //grab the date from the text on the page and create a Date object from the text
  var str = window.$('#shipDate').text().trim();
  var month = parseInt(str.split("-")[1])-1;
  var day = parseInt(str.split("-")[0]) +1;
  var year = parseInt(str.split("-")[2]);
  var date = new Date(Date.UTC(year, month, day));

  //set the date format to match the user's browser preference settings
  var modDate = date.toLocaleDateString();

  //if the month or day are only one digit long, place a leading 0 in front of it then smoosh it all back together again
  modDateArray = modDate.split('/')
  if (modDateArray[0].length == 1) {modDateArray[0] = '0' + modDateArray[0]}
  if (modDateArray[1].length == 1) {modDateArray[1] = '0' + modDateArray[1]}
  modDate = modDateArray[0] + "/" + modDateArray[1] + "/" + modDateArray[2];

  //replace the slashes with dashes because that's how we like them to look
  modDate = modDate.replace(/\//g,'-');

  //update the date on the page to match our formatted date
} catch(e) {


dansullivan 11-18-14

Re: Using Optimizely to convert date format by country

Thanks @JohnH and @nap0leon! I'll probably get a friend to help implement this but I can see how it would work! Thank you!