DEV Community

Free Python Code
Free Python Code

Posted on

How to generate fake times and dates using Python

Hi πŸ™‚πŸ–

In this post, I will use Faker library to generate fake times and dates

Step 1

Install faker

pip install Faker
Enter fullscreen mode Exit fullscreen mode

Step 2

from faker import Faker

f = Faker()

for i in range(20):
    print(f'Time : {f.time()} -- Date : {f.date()}')
Enter fullscreen mode Exit fullscreen mode

Result

Time : 10:38:45 -- Date : 1986-11-17
Time : 16:34:38 -- Date : 2024-03-10
Time : 18:12:26 -- Date : 2011-10-16
Time : 23:03:50 -- Date : 1977-06-27
Time : 05:42:17 -- Date : 1981-05-15
Time : 15:11:53 -- Date : 1981-11-06
Time : 07:37:30 -- Date : 1985-01-24
Time : 05:30:33 -- Date : 1982-12-01
Time : 12:58:50 -- Date : 2012-12-18
Time : 01:37:22 -- Date : 1975-11-05
Time : 12:40:48 -- Date : 2016-02-26
Time : 23:36:48 -- Date : 1977-09-04
Time : 23:38:01 -- Date : 2012-03-26
Time : 16:22:30 -- Date : 1974-04-28
Time : 18:13:26 -- Date : 1982-12-08
Time : 23:48:32 -- Date : 2007-04-25
Time : 23:51:28 -- Date : 2024-04-11
Time : 22:49:50 -- Date : 1996-04-15
Time : 07:51:24 -- Date : 2009-04-20
Time : 07:46:44 -- Date : 1981-08-19
Enter fullscreen mode Exit fullscreen mode

Top comments (0)