Determining whether a date is within daylight savings (Logic App/Flow)

Tom Ashworth
Capgemini Microsoft Blog
3 min readDec 3, 2019

Recently I have needed to understand whether a given date is in Daylight Saving Time or not. As the source of the date didn’t provide time zone information or convert to UTC beforehand and the date destination expected either format, it was considered as UTC resulting in being out by an hour.

The rules

This is surprisingly simple once you know. For the UK (and Central Europe) it is defined as:

In Britain and the European Union, Daylight Saving Time is called British Summer Time (BST). Non-daylight saving time is called British Winter Time (BWT). British Summer Time begins on the last Sunday in March and ends on the last Sunday of October.

For the US, it’s slightly different:

In most of the US, DST starts on the second Sunday of March and ends on the first Sunday of November, at 2:AM both times. The second Sunday in March will always be between the 8th and the 14th inclusive. The first Sunday in November will always be between the 1st and 7th inclusive. The day of week numbering is quite convenient because the day minus day of week will give you the previous Sunday.

The code

I will continue with British Summer Time but you could easily substitute the Sunday limits if required. Below is a TypeScript function that given a date will return true if it’s within DST.

function IsDST(date: Date): boolean {
const month = date.getMonth();
//January, February, November, and December are out.
if (month < 3 || month > 10) { return false; }
//April to September are in
if (month > 3 && month < 10) { return true; }
const previousSunday: number = date.getDate() - date.getDay();
//In March, if previous Sunday was on or after the 25th.
if (month === 3) { return previousSunday >= 25; }
//In October, if previous Sunday was before the 25th.
if (month === 10) { return previousSunday < 25; }
}

Modified from the above stack overflow thread.

I recently learnt you can execute JavaScript code within a Logic App so here it is. (Note: this is in preview)

My problem

This simple solution was exactly what I wanted BUT I was working in a Flow which although based off Logic Apps, there are restrictions and Inline Code is one of them. So, I had to convert the code into a one-liner without variables and then convert that to use the functions available in Logic Apps/Flows.

The one-liner version:

if(or(less(int(formatDateTime(variables('Date'), 'MM')), 3), greater(int(formatDateTime(variables('Date'), 'MM')), 10)), false, if(or(greater(int(formatDateTime(variables('Date'), 'MM')), 3), less(int(formatDateTime(variables('Date'), 'MM')), 10)), true, if(equals(int(formatDateTime(variables('Date'), 'MM')), 3), greaterOrEquals(sub(int(formatDateTime(variables('Date'), 'dd')), dayOfWeek(variables('Date'))), 25), if(equals(int(formatDateTime(variables('Date'), 'MM')), 10), less(sub(int(formatDateTime(variables('Date'), 'dd')), dayOfWeek(variables('Date'))), 25), false ))))

The expanded version for a bit more clarity (comments should be removed):

if(
//January, February, November, and December are out.
or(
less(int(formatDateTime(variables('Date'), 'MM')), 3),
greater(int(formatDateTime(variables('Date'), 'MM')), 10)
),
false,

if(
//April to September are in
or(
greater(int(formatDateTime(variables('Date'), 'MM')), 3),
less(int(formatDateTime(variables('Date'), 'MM')), 10)
),
true,

if(
//In March, if previous Sunday was on or after the 25th.
equals(int(formatDateTime(variables('Date'), 'MM')), 3),
greaterOrEquals(sub(int(formatDateTime(variables('Date'), 'dd')), dayOfWeek(variables('Date'))), 25),

if(
//In October, if previous Sunday was before the 25th.
equals(int(formatDateTime(variables('Date'), 'MM')), 10),
less(sub(
int(formatDateTime(variables('Date'), 'dd')),
dayOfWeek(variables('Date'))),
25),

false
)
)
)
)

I hope this helps others out there with a similar problem. Please comment below if you have had a similar issue or a better solution!

Join the Capgemini Microsoft team and apply for available jobs here.

--

--

Tom Ashworth
Capgemini Microsoft Blog

Tom Ashworth is a professional working within the IT industry as a Software Engineer (Apprentice). Outside of Programming he is loves exploring and photography.