How I can get the current year with the Js module of bucklescript? - reason

I'm working on a ReasonReact project and I need to get the current year with the Js.Date module
I created a function to get a range between two years, so I need to pass the current year as the second parameter
let rangeYears = (startedYear, currentYear) =>
Belt_Array.range(startedYear, currentYear) ->
Array.to_list
|> List.map(
year => FormFieldSelect.{label: Js.Int.toString(year), value: `Int(year)}
)

You can instantiate a new Date type and use getFullYear.
let date = Js.Date.make()
let year = date->Js.Date.getFullYear
or, one-liner
let year = Js.Date.(make()->getFullYear)

Related

Swift XCTAssertEqual() for Date values - how to set part accuracy?

Inside my Unit Test for Dates
let day = StockMarketDay(year: 2022, month: 1, day: 4)
let formatter = ISO8601DateFormatter() // no timezone
formatter.formatOptions = [.withFullDate]
let targetDate = formatter.date(from: "2022-01-04")!
let timeAccuracy = TimeInterval(60*60*24)
let dateAccuracy = Date(timeIntervalSince1970: timeAccuracy)
print("dateAccuracy - \(dateAccuracy)")
// XCTAssertEqual(day!.date, targetDate, accuracy: dateAccuracy) // Error
XCTAssertEqual(day!.date, targetDate, accuracy: timeAccuracy) // Error
I get error: No exact matches in call to global function 'XCTAssertEqual'
It appears that the generic AssertEqual function must have a matching Type (e.g. Date) for the Accuracy parameter. But I don't seem to guess at creating a proper accuracy date to pass in.
Anyone know of a way to Unit Test Dates with a specified accuracy?
It might be worth trying to use TimeIntervals instead of dates, so change the XCTAssertEqual to this:
XCTAssertEqual(day!.date.timeIntervalSince1970, targetDate.timeIntervalSince1970, accuracy: timeAccuracy)

Changing Pentaho date values to first and last day of the week in Javascript Modified Value

I am trying to set two variables to the first and last day of the week for a given date, but the .setDate() method does not seem to be changing the date and 'lastday' and 'firstday' variables return an Invalid Date (1970)
DateNew is from my input step which is defined as dd/MM/yyyy format
var curr = DateNew;
var first = getDayNumber(curr,"d") - getDayNumber(curr,"wm")
var last = first + 7;
var firstday = new Date(curr.setDate(first)).toUTCString();
var lastday = new Date(curr.setDate(last)).toUTCString();
Declaring the input variable as a date seemed to resolve the issue as there was no javascript type associated with it.
var curr = new Date(DateNew);
var first = getDayNumber(curr,"d") - getDayNumber(curr,"wm")
var last = first + 7;
var firstday = new Date(curr.setDate(first)).toUTCString();
var lastday = new Date(curr.setDate(last)).toUTCString();

How can I display current Timezone offset in GMT, in Elm

I'm stuck on getting the current Timezone offset from the Date in Elm.
Date.now
This returns
<Thu Feb 22 2018 20:42:42 GMT+0530 (India Standard Time)> as string
As I have explored in Elm's core lib of date and time, and they don't provide any direct method to fetch the current timezone offset. So what should I do?
import Html as App
import Html exposing (..)
import Date exposing (Date)
import Task
type alias Model =
Maybe Date
type Msg =
SetDate (Maybe Date)
update : Msg -> Model -> (Model, Cmd Msg)
update (SetDate date) _ =
(date, Cmd.none)
view : Model -> Html Msg
view model =
div [] [ text <| dateString model ]
dateString : Model -> String
dateString model =
case model of
Nothing -> "No date here"
Just date ->
(toString <| date)
now : Cmd Msg
now =
Task.perform (Just >> SetDate) Date.now
main : Program Never Model Msg
main =
App.program
{ init = ( Nothing, now )
, view = view
, subscriptions = always Sub.none
, update = update
}
I need this +0530 as in the float 5.5.
Elm's DateTime functions are pretty sparse at the moment, but justinmimbs Date.Extra library is my go to for this type of problem. Check it out here
You can import it as such,
import Date.Extra exposing (offsetFromUtc)
And, then where you had toString <| date change your pipeline to
date
|> offsetFromUtc
|> toString
That'll give you your offset in minutes, if you want the float value, just divide the int by 60. Simple function here to do so:
divBy60 : Int -> Float
divBy60 t =
toFloat t / 60.0
then just change your pipeline again to
date
|> offsetFromUtc
|> divBy60
|> toString

Turning an Int into a String

I'm pretty new to swift (and programming altogether). I'm trying to convert an Int into a String. I've tried using switch statements but every time I use them, it never changes to the String (AKA it prints the number 4) An example of what I'm trying to do is as follows:
class Birthday(_ month: Int, _ day:Int, _ year:Int) -> String{
//Here is where I'd like to turn my month into April
Return (month)
}
let example = Birthday()
example(4,15,1988)
If you really just want to get a month name from a month number, you can do the following:
let formatter = DateFormatter()
let monthName = formatter.monthSymbols[month - 1] // Assuming 1 means January
But since you are passing in a month, day, and year, you presumably want to create a Date and then you want to format that Date into a `String.
Create a Date using Calendar and DateComponents.
let date = Calendar.current.date(from: DateComponents(year: year, month: month, day: day))
Then you format the Date into a String using DateFormatter.
let formatter = DateFormatter()
formatter.dateStyle = .long // choose a desired style
formatter.timeStyle = .none
let string = formatter.string(from: date)
You can use a dictionary which maps objects to each other. For example, a months dictionary could look like:
let months: [Int:String] = [1:"January", 2:"February",...]
return months[4] // returns "April"
Simple solution to get you started would be a method that takes an integer and return your month string.
func numberToMonth(number: Int) -> String {
guard number > 0, number < 13 else { return "" }
return DateFormatter().monthSymbols[number-1]
}

Groovy : Find the date of the previous Monday

In the following code, i want to find the date of the last Monday.
For that, i have two variable :
startDay = today - 7 days
stopDay = today - 1 day (yesterday)
And i have a function that list all dates between "startDay" and "stopDay", and search in these dates, which one corresponds to Monday.
It works well when i have two dates in the same ten :
startDay = 2014-07-20
stopDay = 2014-07-29
But, when one of both change decade, the code end with an error:
startDay = 2014-07-29
stopDay = 2014-07-30
ERROR:
java.lang.IllegalArgumentException: Incompatible Strings for Range: String#next() will not reach the expected value
CODE:
def searchDay = { start, stop -> (start..stop).findAll { Date.parse("yyyy-MM-dd", "${it}").format("u") == "1" } }
def startDay = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date()-7)
def stopDay = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date()-1)
def dateOfTheDay = searchDay(startDay, stopDay);
def dateOfTheDayWithoutSquare = dateOfTheDay.join(", ")
return dateOfTheDayWithoutSquare
This will find the previous Monday starting from today
def cal = Calendar.instance
while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
cal.add(Calendar.DAY_OF_WEEK, -1)
}
Date lastMonday = cal.time
// print the date in yyyy-MM-dd format
println lastMonday.format("yyyy-MM-dd")
If you want to find the Monday previous to some other date replace the first line with:
def cal = Calendar.instance
Date someOtherDate = // get a date from somewhere
cal.time = someOtherDate
This should be a touch faster (no loop):
def cal = Calendar.instance
def diff = Calendar.MONDAY - cal.get(Calendar.DAY_OF_WEEK)
cal.add(Calendar.DAY_OF_WEEK, diff)
cal.time.format("yyyy-MM-dd")