Jump to content

Setting locale of date works on w3schools but not in script


ender

Recommended Posts

Hello. I am using the following script to add a note to the top of the document of the date when the document was last modified. I have attempted to set the format to the GB locale but the date is displaying in the US locale.

function PutDateModified()
{
  var date = document.lastModified.substr(0, document.lastModified.lastIndexOf(" "));
  var d = date.toLocaleString("en-GB")
  document.write('<div style="font-size: 0.9em; text-align:right; color: blue;;">');
  document.write("<i>Page Last Updated: " + d + "</i>");
  document.write("</div>"); 
}

I have tried using the methods getDate, getMonth and getFullYear and adding those values separately to what is written in the document but the result is not recognised by Firefox and nothing is displayed. What I tried was as follows :

var gd = date.getDate()
var gm = date.getMonth()
var fy = date.getFullYear()
//...
document.write("<i>Page Last Updated: " + gd + "/" + gm + "/" + fy + "</i>");

Any help welcome

  • Like 1
Link to comment
Share on other sites

The issue is that this line returns a string, not a date:

var date = document.lastModified.substr(0, document.lastModified.lastIndexOf(" "));

You can't use date methods on a string, you have to create a date object.

  • Like 1
Link to comment
Share on other sites

Thank you. Is there any way I can alter that line so that the string is in en-GB format? I have instantiated a date object using

var d = new Date(date)

but I haven't been able to use the components date, month and full year to create a string recognised by Firefox.

Edited by ender
  • Like 1
Link to comment
Share on other sites

This code works correctly for me:

var dateString = document.lastModified.substr(0, document.lastModified.lastIndexOf(" "));
var date = new Date(dateString);
console.log(date.toLocaleString());

What does your code currently look like?

  • Like 1
Link to comment
Share on other sites

Thank you. The code I have displays the date in US format and includes a zero number for time.

function PutDateModified()
{
  var date = document.lastModified.substr(0, document.lastModified.lastIndexOf(" "));
  var d = new Date(date)
  var l = d.toLocaleString("en-GB")
  document.write('<div style="font-size: 0.9em; text-align:right; color: blue;;">');
  document.write("<i>Page Last Updated: " + l + "</i>");
  document.write("</div>"); 
}

If I use your code, nothing is displayed in Firefox :

function PutDateModified()
{
  var dateString = document.lastModified.substr(0, document.lastModified.lastIndexOf(" "));
  var date = new Date(dateString)
  console.log(date.toLocaleString()
  document.write('<div style="font-size: 0.9em; text-align:right; color: blue;;">');
  document.write("<i>Page Last Updated: " + l + "</i>");
  document.write("</div>"); 
}
Edited by ender
  • Like 1
Link to comment
Share on other sites

Hello again. I achieved a GB locale format by putting this code at the bottom of another page, defining a paragraph id ("datemod") and adding a paragraph class (p.dm) to the .css document. It shows the date string in the document in GB format but, unfortunately, with a zero time which I don't want.

<script type="text/javascript">
var dateString = document.lastModified.substr(0, document.lastModified.lastIndexOf(" "));
var date = new Date(dateString)
var l = date.toLocaleString("en-GB")
var s = "<i>Document last updated: " + l + "</i>"
document.getElementById("datemod").innerHTML=s;
</script>
Edited by ender
  • Like 1
Link to comment
Share on other sites

This code is now working with the function in a .js document :

function PutDateModified()
{
  var dateString = document.lastModified.substr(0, document.lastModified.lastIndexOf(" "));
  var date = new Date(dateString)
  var l = date.toLocaleString("en-GB")
//  console.log(date.toLocaleString()
  document.write('<div style="font-size: 0.9em; text-align:right; color: blue;;">');
  document.write("<i>Page Last Updated: " + l + "</i>");
  document.write("</div>"); 
}

Thank you for the help.

  • Like 1
Link to comment
Share on other sites

I have further amended the code as follows to get the format Wed Oct 12 2016 :

function PutDateModified()
{
  var dateString = document.lastModified.substr(0, document.lastModified.lastIndexOf(" "));
  var date = new Date(dateString)
  var d = date.toDateString()
//  var l = date.toLocaleString("en-GB")
//  console.log(date.toLocaleString()
  document.write('<div style="font-size: 0.9em; text-align:right; color: blue;;">');
  document.write("<i>Page Last Updated: " + d + "</i>");
  document.write("</div>"); 
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...