Current Week Number - Multiple Languages

Created by
Saturday, June 4, 2016

Simple code snippets that outline how to call the current week number in multiple different programming languages.

 

C#

var culture = CultureInfo.GetCultureInfo("cs-CZ");
var dateTimeInfo = DateTimeFormatInfo.GetInstance(culture);

var dateTime = DateTime.Today;

int weekNumber = culture.Calendar.GetWeekOfYear(dateTime, dateTimeInfo.CalendarWeekRule, dateTimeInfo.FirstDayOfWeek);

PHP

$currentWeekNumber = date('W');
echo 'Week number:' . $currentWeekNumber;

Java

Calendar calendar = new GregorianCalendar();
Date trialTime = new Date();   
calendar.setTime(trialTime);     
System.out.println("Week number:" + 
    calendar.get(Calendar.WEEK_OF_YEAR));

Python

from datetime import date

weekNumber = date.today().isocalendar()[1]
print 'Week number:', weekNumber

MySql

SELECT WEEKOFYEAR(NOW());


Javascript

new Date().format('W');
4
Rating
1 2 3 4 5

Save

Saved

Report

Reported
Reasons
Cancel
0 comments