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.

65 thoughts on “Happy 2011: In 10+ Different Programming Languages

  1. Tweets that mention Happy 2011: In 10 Different Programming Languages -- Topsy.com

    • Seriously Reuben, you gotta do better than this :) Brainfuck is cool, I have no issues against it, but simply printing 2011 on screen is not cool. Remember I mentioned in the beginning of this post that the reason I'm writing it is 'cause lot of people hard-code copyright dates?

      Thanks for your commitment anyway ;) I had fun testing your code.

    • Note that the following is also valid Objective-C. If you compile with -fobjc-gc, the garbage collector will be enabled (does not work on iOS AFAIK).

      #import

      int main (int argc, const char * argv[]) {
      NSLog (@"%d", [[NSCalendarDate date] yearOfCommonEra]);
      return 0;
      }

  2. Your UNIX Shell example is much too long. Correct is:

    date +%Y

    which makes it the shortest of all the ones here.

    And your Perl example could be:

    print (localtime)[5] + 1900;

    • Thanks Joshua, although the Perl one-liner didn't work gave a syntax error so I had to simplify the two-liner. Don't know why, I'm not a perl programmer ;)

      Cheers!

  3. The JavaScript example shouldn't really be wrapped in the 'document.write' function. That's part of a web-browsers DOM, rather than JavaScript itself.

    • So what do you think is the best way to actually output it on screen? I'm sort of trying to keep the same pattern for all the snippets, they output something on screen :)

  4. Much simpler java version. No imports needed:

    public class Happy {
    public static void main(String[] args) {
    System.out.printf("%Y
    ", System.currentTimeMillis());
    }
    }

  5. in Css:

    body:after{
    content:"happy new year 2011";
    }

    in oracle pl/sql:

    select extract(year from sysdate)
    from dual

    • in Css:

      body:after{
      content:'happy new year 2011'; // edited one quotation
      }

      in oracle pl/sql:

      select extract(year from sysdate)
      from dual

  6. // D
    import std.stdio, std.date;
    void main(string[] args)
    {
    writeln(yearFromTime(UTCtoLocalTime(getUTCtime())));
    }

    • Since the other examples do omit the compiler/interpreter commands, the scala example becomes simply this:

      printf("%TY
      ", System.currentTimeMillis)

  7. I think the Java version is a bit too long. How about :
    public class HappyNewYear {
    public static void main(String[] args) {
    System.out.println(new java.util.GregorianCalendar().get(java.util.Calendar.YEAR));
    }
    }

Comments are closed.