w3resource logo


CoffeeScript exercises

CoffeeScript: Get the current date

CoffeeScript : Exercise-2 with Solution

Write a CoffeeScript program to get the current date.

Expected Output :
mm-dd-yyyy, mm/dd/yyyy or dd-mm-yyyy, dd/mm/yyyy

Solution :

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="//jashkenas.github.io/coffee-script/extras/coffee-script.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>

</body>
</html>
  

CoffeeScript Code :

today = new Date
dd = today.getDate()
#The value returned by getMonth is an integer between 0 and 11, referring 0 to January, 1 to February, and so on.
mm = today.getMonth() + 1
yyyy = today.getFullYear()
if dd < 10
  dd = '0' + dd
if mm < 10
  mm = '0' + mm
today = mm + '-' + dd + '-' + yyyy
console.log today
today = mm + '/' + dd + '/' + yyyy
console.log today
today = dd + '-' + mm + '-' + yyyy
console.log today
today = dd + '/' + mm + '/' + yyyy
console.log today

Live Demo :

JS Bin on jsbin.com

Post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.