Member-only story

Happy International Programmers’ Day!

One-liner to find out the day of the year — 256th day

Yong Cui
2 min readSep 13, 2021
Photo by Ambreen Hasan on Unsplash

Hello folks! It’s the 256th day of this year, which is the Day of the Programmer. Certainly, I didn’t use my fingers and toes to count that, because I only can count to 20 with them:)

The reason why 256 (2**8) was chosen because that’s the number of distinct values that a byte can hold. Find out more about the date at Wikipedia.

Here’s a small Python code snippet to show you how to do the calculation with just one line of code (two, if you consider the import statement line).

>>> import datetime
>>> datetime.datetime.now().timetuple().tm_yday
256

How about JavaScript? Here you go.

>> today = new Date()
<- Date Mon Sep 13 2021 13:32:58 GMT-0500 (Central Daylight Time)
>> Math.floor((today - new Date(today.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24)
<- 256

Different languages have their own ways to find out the day of the year. Please feel free leave a comment to show us how to do it with the language that you’re most familiar with.

--

--

Yong Cui
Yong Cui

Written by Yong Cui

Work at the nexus of biomedicine, data science & mobile dev. Author of Python How-to by Manning (https://www.manning.com/books/python-how-to).

No responses yet