Answer the question
In order to leave comments, you need to log in
How to subtract the current date from the date entered by the user?
The task is this: I have to compare two dates (date - from the user, and the current date currentTime) Take the date from the user and subtract the current date - and get the difference in days. And this difference should be no more than 16 That is, the date from the user should be not earlier than the current one (so as not to fly into the past), but not later than "current + 16 days"
getDateFromUser :: IO (Either UserError Date)
getDateFromUser = do
Prelude.putStrLn "Пожалуйста, укажите дату для прогноза в формате ГГГГ-ММ-ДД:"
date <- Prelude.getLine
currentTime <- getCurrentTime
let forecastDay = parseTimeM True defaultTimeLocale "%Y-%m-%d" date :: Maybe Day
if date >= ...
then return $ Right $ (Data.Text.pack date)
else return $ Left InvalidDate
Answer the question
In order to leave comments, you need to log in
compare two dates [...] subtract the current date - and get the difference in days. And this difference should be no more than 16. That is, the date from the user should not be earlier than the current one (so as not to fly into the past), but no later than "current + 16 days"
-- количество дней (но не меньше 0 и не больше 16) от сего дня (today) до указанного дня (day)
-- возвращаемое число лежит в интервале [0, 16]
strippedDayDiff :: Day -> Day -> Integer
strippedDayDiff day today = let d = diffDays day today in if d > 16 then 16 else if d < 0 then 0 else d
if strippedDayDiff forecastDay (utctDay currentTime) >= ...
I do not know haskell
But the essence is this:
1. Take the current unixtimestamp
2. Convert the date from the user to unixtimestamp
3. Subtract the second from the first
4. Convert the date again from the resulting result.
As I understand it, there are functions for all this .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question