-3

I am new with javascript, so maybe I missed the obvious.

I have the following script (see also some other answer):

<script>
var d = new Date();
document.getElementById("demo").innerHTML = d.toLocaleFormat("DD.MM.YYYY");
</script>

which I want to use to create a formatted date in jacasvript. If the date is today (26 June 2015) I expect this script to produce the following string:

26.06.2015

However, what I get is the following:

DD.MM.YYYY

Is the other answer wrong? How can I get this formatted date without some extra library?

I also tried to use parse or format instead without success.

Community
  • 1
  • 1
Alex
  • 41,580
  • 88
  • 260
  • 469
  • 1
    Because you're using the wrong format specifiers. [These are the ones to use](http://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html). Also note that it's a completely non-standard function, that won't work across all browsers. This is why libraries such as moment.js exist. – James Thorpe Jun 26 '15 at 10:42
  • 1
    `toLocaleFormat` is not standard. Use `toLocaleDateString`. – mostruash Jun 26 '15 at 10:43
  • Maybe you could provide a working example? – Alex Jun 26 '15 at 10:43
  • `toLocaleDateString` produces an error `invalid language tag: DD.MM.YYYY`. – Alex Jun 26 '15 at 10:44
  • 2
    [Here's the documentation for `toLocaleDateString`.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString) – Pointy Jun 26 '15 at 10:47
  • @Pointy: Can you give a working example? – Alex Jun 26 '15 at 10:48
  • 1
    Maybe you can read the docs and do it yourself. – JJJ Jun 26 '15 at 10:48
  • I read the docs, and I am sure the function `toLocateDateString` cannot solve my problem! – Alex Jun 26 '15 at 10:49

3 Answers3

5

Try this code:

    <script>
        var d = new Date();
        document.getElementById("demo").innerHTML = d.toLocaleFormat("%d.%m.%Y");
   </script>
Swapnil Punekar
  • 197
  • 1
  • 10
0

As mentioned here you can create your own function.

  var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!

    var yyyy = today.getFullYear();
    if(dd<10){
        dd='0'+dd
    } 
    if(mm<10){
        mm='0'+mm
    } 
    var today = dd+'/'+mm+'/'+yyyy;
    document.getElementById("demo").innerHTML  = today;
Community
  • 1
  • 1
cs04iz1
  • 1,737
  • 1
  • 17
  • 30
0
Try This code 

<p>Click the button to display the date as a string.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var d = new Date();
    var n = d.toLocaleDateString();
    document.getElementById("demo").innerHTML = n;
}
</script>
shas
  • 703
  • 2
  • 8
  • 31