javascript display date code example

Example 1: php change date format

To convert the date-time format PHP provides strtotime() and date() function. We change the date format from one format to another.

Change YYYY-MM-DD to DD-MM-YYYY
<? php.
$currDate = "2020-04-18";
$changeDate = date("d-m-Y", strtotime($currDate));
echo "Changed date format is: ". $changeDate. " (MM-DD-YYYY)";
?>

Example 2: vue print date

import moment from 'moment'

Vue.filter('formatDate', function(value) {
  if (value) {
    return moment(String(value)).format('MM/DD/YYYY hh:mm')
  }
}

Example 3: get current time in js

<script>

var today = new Date();

var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();

</script>

Example 4: new date() javascript

var date = new Date(); //Will use computers date by default.
//parameters will specify date you put to input
var date = new Date(year, month, day, hours, minutes, seconds, milliseconds);

Example 5: calculate today date javascript

var today = new Date();

Example 6: display date in javascript

<button onclick="displayDate()">The time is?</button>

<script>
function displayDate() {
  document.getElementById("demo").innerHTML = Date();
}
</script>