Happy 2011: In 10+ Different Programming Languages

The end of the year 2010 is near so I’ve prepared this post for all you coders, developers and other geeks. You know when you build applications (especially web applications) you often leave a copyright notice on every page saying “Copyright 2010, All Rights Reserved” or whatever? Right, but what developers tend to forget is that time passes by and that they have to go back and change 2010 to 2011 in January — so we often browse websites, especially in January and February that have last year’s copyrights.

The best trick is not to hardcode the year inside your templates and skins, but to write the current year dynamically using date functions, so below is a list of printing out the current year in 10 different programming languages.

And here’s a list of ones contributed by commentators:

Python

from datetime import date
print date.today().year

PHP

Definitely one of the easiest ways.

echo date("Y");

C and C++

#include <stdio.h>
#include <time.h>

int main() {
        time_t now = time(NULL);
        struct tm* local = localtime(&now);
        printf("%d", local->tm_year + 1900);
        return 0;
}

JavaScript

document.write(new Date().getFullYear());

Perl

my $year = (localtime)[5];
print $year + 1900;

Ruby

puts Time.now.year

Java

import java.util.*;
import java.text.*;

public class Apollo {
	public static void main(String[] args) {
		Date date = new Date();
		SimpleDateFormat simpleDateformat=new SimpleDateFormat("yyyy");
		System.out.println(simpleDateformat.format(date));
	}
}

Unix Shell

Seems to be the shortest one of all.

date +%Y

Go (golang)

Interesting fact that when I ran this code on the Go website I ended up with 2009 — guess their servers are running a little slow?

package main

import ("fmt"
	"time")

func main() {
	var t = time.LocalTime()
	fmt.Println(t.Year)
}

x86 Assembly

Here’s a special one, donated by my brother @SoulSeekah.

jmp start

year db "0000$"

start:
	mov ah, 04h
	int 1Ah

	mov bh, ch
	shr bh,4
	add bh,30h
	mov [year], bh
	mov bh,ch
	and bh,0fh
	add bh,30h
	mov [year+1],bh

	mov bh, cl
	shr bh,4
	add bh,30h
	mov [year+2], bh
	mov bh,cl
	and bh,0fh
	add bh,30h
	mov [year+3],bh

	mov ah,09h
	mov dx, offset year
	int 21h

	mov ah,4Ch
	mov al,00
	int 21h

Lua

Code by Vesa Marttila (@ponzao).

print(os.date("*t").year)

Clojure

Once again thanks to Vesa Marttila (@ponzao).

(let [date (java.util.Date.)
      sdf  (java.text.SimpleDateFormat. "yyyy")]
  (println (.format sdf date)))

Objective-C

Donated by Lowell via the comments section.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSLog (@"%d", [[NSCalendarDate date] yearOfCommonEra]);

    [pool drain];
    return 0;
}

C# (C-Sharp)

Contributed by a certain Paul via the comments.

using System;

class Program {
    static void Main(string[] args) {
        Console.WriteLine(DateTime.Now.Year);
    }
}

Tcl

Contributed by Robert.

set year [clock format [clock seconds] -format {%Y}]
puts $year

Adobe Flex

Seems to work with both Flex 3 and 4, contributed by Uber_Nick via reddit.com comments.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Label text="{new Date().fullYear}" />
</mx:Application>

Delphi

Thanks to Rejn via comments and Reddit.

ShowMessage( IntToStr(YearOf(Now)) );

Haskell

Thanks to Pezezin via comments and Onmach via Reddit.

import System.Time
main = getClockTime >>= toCalendarTime >>= print . ctYear

Oh, and here’s a special one in LOLCODE by Danita via Reddit.

That’s about it! Feel free to contribute to the list in any other programming language, preferably well-known and easy to test. Use pastebin.com and leave links to your code in the comments section below. Have a Merry Christmas and a Happy New Year. Welcome to twenty-eleven! See you next year! Oh, and thank you all for tweeting this!

P.S. Christmas is on January 7th in Russia, so we’re not going to have holidays until the 31st.

About the author

Konstantin Kovshenin

WordPress Core Contributor, ex-Automattician, public speaker and consultant, enjoying life in Moscow. I blog about tech, WordPress and DevOps.

65 comments