w3resource

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

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="//jashkenas.github.io/coffee-script/extras/coffee-script.js"></script>
  <meta charset="utf-8">
  <title>Get the current date</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

Sample Output:

"03-20-2018"
"03/20/2018"
"20-03-2018"
"20/03/2018"

Live Demo :

See the Pen coffeescript-exercise-2 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/coffeescript-exercises/coffeescript-exercise-2.php