w3resource logo


CoffeeScript exercises

CoffeeScript: Display the current day and time in the specific format

CoffeeScript Basic : Exercise-1 with Solution

Write a CoffeeScript program to display the current day and time in the following format.

Sample Output : Today is : Friday.
Current time is : 4 PM : 50 : 22

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
day = today.getDay()
daylist = [
  'Sunday'
  'Monday'
  'Tuesday'
  'Wednesday '
  'Thursday'
  'Friday'
  'Saturday'
]
console.log 'Today is : ' + daylist[day] + '.'
hour = today.getHours()
minute = today.getMinutes()
second = today.getSeconds()
prepand = if hour >= 12 then ' PM ' else ' AM '
hour = if hour >= 12 then hour - 12 else hour
if hour == 0 and prepand == ' PM '
  if minute == 0 and second == 0
    hour = 12
    prepand = ' Noon'
  else
    hour = 12
    prepand = ' PM'
if hour == 0 and prepand == ' AM '
  if minute == 0 and second == 0
    hour = 12
    prepand = ' Midnight'
  else
    hour = 12
    prepand = ' AM'
console.log 'Current Time : ' + hour + prepand + ' : ' + minute + ' : ' + second

Live Demo :

JS Bin

Post your code through Disqus :



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

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program