A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.w3resource.com/coffeescript-exercises/coffeescript-exercise-1.php below:

Website Navigation


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

CoffeeScript: Display the current day and time in the specific formatLast update on November 14 2023 07:18:51 (UTC/GMT +8 hours) Coffee Script 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

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="//jashkenas.github.io/coffee-script/extras/coffee-script.js"></script>
  <meta charset="utf-8">
  <title>Display the current day and time in the specific format</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

Sample Output:

"Today is : Tuesday."
"Current Time : 1 PM  : 52 : 30"

Live Demo:

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

Improve this sample solution and post your code through Disqus.




RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4