- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Haskell Program to Check the birthday and print Happy Birthday message
In Haskell, this program will help us in checking the current date against a specified birthday and prints a "Happy Birthday" message if it is a match otherwise, it will print “Not your birthday yet”. Haskell provides function to fetch the current date and also to specify a date. This program can be implemented by using various approaches including the use of toGregorian function, fromGregorian function or by using utctDay <$> getCurrentTime.
This program will only check the birthday once, when the program runs. If we want the program to check the birthday periodically or at a specific time, we can use a library like timer or schedule, to schedule the check at a specific interval.
Method 1: Using toGregorian function
This method uses the Data.Time library to get the current date and the toGregorian function to extract the day and month of the current date.
Then it compares the current day and month to the specified birthday, and if they match, it prints "Happy Birthday!" otherwise it prints "Not your birthday yet."
Algorithm
Step 1 − Data.Time module is imported.
Step 2 − The specified birthday is defined as a day and month..
Step 3 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do..
Step 4 − The current date is fetched by using getCurrentTime and toGregorian function..
Step 5 − The current day and month is compared to the birthday specified above and final output is displayed.
Example
In the following example. We are going to check the birthday and print Happy Birthday message using toGregorian function.
import Data.Time birthday :: (Int, Int) birthday = (12, 25) main :: IO () main = do today <- getCurrentTime let (_, month, day) = toGregorian $ utctDay today if (month, day) == birthday then putStrLn "Happy Birthday!" else putStrLn "Not your birthday yet."
Output
Not your birthday yet.
Method 2: Using fromGregorian function
In this method , the getZonedTime function is used to get the current date and time in the time zone, and the zonedTimeToLocalTime function is used to extract the local date and time, which is stored in now_time variable. The fromGregorian function is used to create a Day value from year, month and day integers, then LocalTime data constructor is used to create a LocalTime value by combining the Day value with a TimeOfDay value.
Algorithm
Step 1 − Data.Time.LocalTime and Data.Time.Calendar modules are imported.
Step 2 − The birthday is defined as a LocalTime by using fromGregorian function.
Step 3 − Program execution will be started from main function. The main() function has whole control of the program.
Step 4 − The current date is fetched and compared.
Step 5 − The final output is displayed after the comparison.
Example
In this example, we are going to check the birthday and print Happy Birthday message using fromGregorian function
import Data.Time.LocalTime import Data.Time.Calendar birthday :: LocalTime birthday = LocalTime (fromGregorian 2022 12 25) (TimeOfDay 12 0 0) main :: IO () main = do now <- getZonedTime let now_time = zonedTimeToLocalTime now if now_time == birthday then putStrLn "Happy Birthday!" else putStrLn "Not your birthday yet."
Output
Not your birthday yet.
Method 3: using fromGregorian function and utctDay <$> getCurrentTime.
In this method , the Data.Time module and it's Day type which is a newtype wrapper around a Integer representing days since the epoch (January 1, year 1) is used. The fromGregorian function is used to create a Day value from year, month and day integers. And the utctDay <$> getCurrentTime is used to get the current date and time in UTC, and extracts the day from the current date. Then it compares the current day to the specified birthday, and if they match, it prints "Happy Birthday!" otherwise it prints "Not your birthday yet."
Algorithm
Step 1 − Data.Time module is imported.
Step 2 − The specified birthday is defined as a day using fromGregorian function.
Step 3 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.
Step 4 − The current date is fetched using utctDay <$> getCurrentTime.
Step 5 − The final output is displayed on comparing the specified birthday and current date fetched.
Example
In this example, we are going to using fromGregorian function and utctDay <$> getCurrentTime
import Data.Time birthday :: Day birthday = fromGregorian 2022 12 25 main :: IO () main = do today <- utctDay <$> getCurrentTime if (today == birthday) then putStrLn "Happy Birthday!" else putStrLn "Not your birthday yet."
Output
Not your birthday yet.
Conclusion
In Haskell, the specified birthday is defined and current date is fetched. And this specified birthday is being compared by the current date fetched, and if it is same then output is printed as Happy Birthday message or else it will display Not your birthday yet. It can be achieved by various approaches including the use of toGregorian function, fromGregorian function or by using utctDay <$> getCurrentTime.
- Related Articles
- Java Program to check the birthday and print Happy Birthday message
- C++ program to print Happy Birthday
- Birthday Paradox in C++
- Birthday Paradox in Python
- Birthday attack in Cryptography
- Birthday Reminder Application in Python
- How to Calculate Age in Excel from Birthday?
- How to Generate Random Birthday Wishes using JavaScript?
- How to calculate days until next birthday in Excel?
- Python program to print all Happy numbers between 1 and 100
- Why do you celebrate your birthday every year?
- How to calculate the next age of a person’s birthday in Excel?
- Haskell Program to print Hello World!
- Haskell program to print ascii values
- Python program to check if the given number is Happy Number
