Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 134499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:37:28+00:00 2026-05-11T06:37:28+00:00

How would I generate a random date that has to be between two other

  • 0

How would I generate a random date that has to be between two other given dates?

The function’s signature should be something like this:

random_date('1/1/2008 1:30 PM', '1/1/2009 4:50 AM', 0.34)                    ^                       ^          ^              date generated has  date generated has  a random number             to be after this    to be before this 

and would return a date such as: 2/4/2008 7:20 PM

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:37:29+00:00Added an answer on May 11, 2026 at 6:37 am

    Convert both strings to timestamps (in your chosen resolution, e.g. milliseconds, seconds, hours, days, whatever), subtract the earlier from the later, multiply your random number (assuming it is distributed in the range [0, 1]) with that difference, and add again to the earlier one. Convert the timestamp back to date string and you have a random time in that range.

    Python example (output is almost in the format you specified, other than 0 padding – blame the American time format conventions):

    import random import time      def str_time_prop(start, end, time_format, prop):     """Get a time at a proportion of a range of two formatted times.      start and end should be strings specifying times formatted in the     given format (strftime-style), giving an interval [start, end].     prop specifies how a proportion of the interval to be taken after     start.  The returned time will be in the specified format.     """      stime = time.mktime(time.strptime(start, time_format))     etime = time.mktime(time.strptime(end, time_format))      ptime = stime + prop * (etime - stime)      return time.strftime(time_format, time.localtime(ptime))   def random_date(start, end, prop):     return str_time_prop(start, end, '%m/%d/%Y %I:%M %p', prop)      print(random_date("1/1/2008 1:30 PM", "1/1/2009 4:50 AM", random.random())) 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to generate a random floating point number between 2 values. What
I would like to generate random numbers between 1 & 10, with a decimal
I need to generate random text strings of a particular format. Would like some
I'm trying to write some LINQ To SQL code that would generate SQL like
How would you generate an JPG image file containing data fields that are stored
I'm trying to generate a random date of birth for people in my database
Does anyone know of an app that would generate an xml file filled out
I'm in a situation where I would to generate a script for a database
I would like to generate a WSDL file from a c++ atl webservice without
I would like to generate a list of files within a directory. Some of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.