0

I have read a lot of thread about the subject, but i'm not much into programming... i've made some tests but never acheive what i wanted.

Here we have a html template we have to modify to suit our needs.

here the code i have to edit:

<p id="DATE"></p>
<script>
    var d = new Date();
    document.getElementById("DATE").innerHTML = d.toString();
</script>

Its giving me the date OK...but not i the format i want... the format i want is... DD-MM-YYYY HH:MM

Simple has that :)

Its been a couple of hours and i can't do it... Can anyone help with a code i just can copy/paste in my html file.

lin
  • 17,956
  • 4
  • 59
  • 83
  • 1
    You should use momentjs https://momentjs.com/. Very easy to parse and format dates: `moment().format('MMMM Do YYYY, h:mm:ss a')` – Rob M. Mar 13 '17 at 22:56
  • There is a vanilla Javascript answer [here](http://stackoverflow.com/questions/12409299/how-to-get-current-formatted-date-dd-mm-yyyy-in-javascript-and-append-it-to-an-i) – symlink Mar 13 '17 at 23:00
  • no thanks... i've seen this answer too much.. – Laurent Soucy Mar 13 '17 at 23:00
  • there is already an answer here --> http://stackoverflow.com/questions/30891884/how-to-convert-string-dd-mm-yy-hhmmss-to-date-in-javascript please make sure you search StackOverflow for questions first before posting. – Ousmane D. Mar 13 '17 at 23:01
  • I've seen dozens of thread taking about this... but i not good enough to implement this in my page... i mean, i'm tryng to copy paste section of codes, but its not working on my end. I'm really not good at programming. – Laurent Soucy Mar 13 '17 at 23:11

3 Answers3

1

As opposed to using an external JavaScript library for a simple task. You should be able to achieve the same with Vanilla js.

Refer to this: Where can I find documentation on formatting a date in JavaScript?

In short:

var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();
console.log(curr_date + "-" + curr_month + "-" + curr_year + " " + curr_hour + ":" + curr_min);

The above snippet should have all the code you need.

For your specific implementation, you will need to do the below:

<script>
    var d = new Date();
    var curr_date = d.getDate();
    var curr_month = d.getMonth() + 1; //Months are zero based
    var curr_year = d.getFullYear();
    var curr_hour = d.getHours();
    var curr_min = d.getMinutes();
    var formattedDate = curr_date + "-" + curr_month + "-" + curr_year + " " + curr_hour + ":" + curr_min;
    document.getElementById("DATE").innerHTML = formattedDate;
</script>
0

A function for the format you require is fairly simple. The following will return a string for a provided Date, or the current date if none passed.

function formatDate(date) {
  var d = date || new Date();
  function z(n){return (n<10?'0':'')+n}
  return z(d.getDate()) + '-' + 
         z(d.getMonth()+1) + '-' +
         d.getFullYear() + ' ' +
         z(d.getHours()) + ':' +
         z(d.getMinutes());
}
document.write(formatDate());

BTW, you can't use the same token for two separate parts. While there is no standard or even consistency for formatting tokens, your format would be more commonly expressed as:

DD-MM-YYYY HH:mm

Which is compatible with moment.js and a few others, though there are many other token sets to represent the same format.

RobG
  • 142,382
  • 31
  • 172
  • 209
0

To achieve your goal simply try following codes:

First of all, inside the head tag add the below line of code:

<script src="https://rawgit.com/moment/moment/2.2.1/min/moment.min.js"></script>

Finally, change your existing line of code:

document.getElementById("DATE").innerHTML = moment(d).format('DD-MM-YYYY HH:MM');

Happy coding :)

Rashed Rahat
  • 2,357
  • 2
  • 18
  • 38