Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

current year and quantity days in month

Tags:

date

time

haskell

I have two questions:

How can I get current year (not date) in haskell ?

How can I get quantity days in some month some year ? For example 04.2011 has got 30 days.

like image 247
mrquestion Avatar asked Apr 20 '11 11:04

mrquestion


People also ask

How do you calculate current days in month?

To get the number of days in the current month:function getDaysInCurrentMonth() { const date = new Date(); return new Date( date. getFullYear(), date. getMonth() + 1, 0 ). getDate(); } const result = getDaysInCurrentMonth(); console.

How get number of days in a month in PHP?

The cal_days_in_month() function returns the number of days in a month for a specified year and calendar.

How do you know if a month has 30 or 31 days JavaScript?

JavaScript getDate() Method: This method returns the number of days in a month (from 1 to 31) for the defined date. Return value: It returns a number, from 1 to 31, representing the day of the month.


1 Answers

This will give you the year, month and day:

import Data.Time.Clock
import Data.Time.Calendar

date :: IO (Integer,Int,Int) -- :: (year,month,day)
date = getCurrentTime >>= return . toGregorian . utctDay

Source: http://www.haskell.org/haskellwiki/Date

There's a function in Date.Time.Calendar called gregorianMonthLength with type Integer -> Int -> Int. It takes a year and a month as arguments, returns the number of days, just like you need. http://www.haskell.org/ghc/docs/7.0.2/html/libraries/time-1.2.0.3/Data-Time-Calendar.html

A full example:

import Data.Time.Clock
import Data.Time.Calendar

date :: IO (Integer,Int,Int) -- :: (year,month,day)
date = getCurrentTime >>= return . toGregorian . utctDay

main = do
    (year, month, day) <- date
    putStrLn $ "Days in this month: " ++ (show $ gregorianMonthLength year month)
like image 65
Tomas Avatar answered Oct 29 '22 11:10

Tomas