Fable: Compilation error: FABLE: Cannot resolve System.Globalization.Calendar.GetWeekOfYear

Created on 29 Oct 2020  路  5Comments  路  Source: fable-compiler/Fable

Description

I'm trying to get the week number of a date using the System.Globalization.Calendar.GetWeekOfYear function (https://docs.microsoft.com/en-us/dotnet/api/system.globalization.calendar.getweekofyear?view=netcore-3.1)
The code below does not compile using the fable-compiler (2.13.0) while it does compile using dotnet build

Repro code

The following code causes the error in the REPL:

open System.Globalization
open System

CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(DateTime(1,1,2020), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)

Expected and actual results

Expected: returns integer corresponding to week number of specified date.
Actual: throws the following error:
FABLE: Cannot resolve System.Globalization.CultureInfo.get_CurrentCulture

Related information

local fable-compiler version: 2.13.0
REPL fable-compiler version: 2.10.1
operating system: Windows 10 (error is reproducible in REPL)

Most helpful comment

@stan-kroon Also related :wink: you can install Fable.DateFunctions (along with its dependency date-fns) and then calculate which week it is like this

open System
open Fable.DateFunctions

let now = DateTime.Now
let currentWeekNumber = now.DifferenceInCalendarWeeks(now.StartOfYear())

At least, I think this will work :smile:

There is also DifferenceInCalendarISOWeeks extension method, maybe you can try that one

All 5 comments

Hello @stan-kroon,

Fable doesn't provide support for all the BCL API's and the "Globalization" API is one of them.

I don't think that support for it will be included in Fable.

The reason, is that we don't always have the equivalent available in native JavaScript and also it could be a lot of work to do and maintains.

Thanks for your response Maxime, makes sense!

Maybe related, there's a proposal to support CultureInfo #2126. It's not a priority at the moment, but of course anybody wanting to give it a try is more than welcome :)

@stan-kroon Also related :wink: you can install Fable.DateFunctions (along with its dependency date-fns) and then calculate which week it is like this

open System
open Fable.DateFunctions

let now = DateTime.Now
let currentWeekNumber = now.DifferenceInCalendarWeeks(now.StartOfYear())

At least, I think this will work :smile:

There is also DifferenceInCalendarISOWeeks extension method, maybe you can try that one

Thanks Zaid-Ajaj, that is exactly what I need!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

funlambda picture funlambda  路  4Comments

MangelMaxime picture MangelMaxime  路  3Comments

forki picture forki  路  3Comments

alfonsogarciacaro picture alfonsogarciacaro  路  3Comments

SirUppyPancakes picture SirUppyPancakes  路  3Comments