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

  • SEARCH
  • Home
  • 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 8357517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:30:10+00:00 2026-06-09T10:30:10+00:00

I am trying to add seconds to python datetime excluding weekends using pandas. The

  • 0

I am trying to add seconds to python datetime excluding weekends using pandas. The code below works, but I would like to know if there is a simpler way to achieve this.

import datetime
from pandas import *
from pandas.tseries.offsets import *

def add_seconds(start_date, offset_in_seconds):
# get input date in datetime
d = datetime.strptime(start_date, '%Y-%m-%d %H:%M:%S')

# get days, hours, mins, secs
    no_of_days, remainder = divmod(offset_in_seconds, 86400)
    hours, minutes = divmod(remainder, 3600)
    minutes, seconds = divmod(minutes, 60)

# increment the input date  to the appropriate business day
    end_date_pre = d + no_of_days*BDay() 

# dial back to previous evening if hour is under 24
    if 16 + hours < 24:
    end_date = end_date_pre 
    new_end_date = datetime(end_date.year, end_date.month, end_date.day, 16, 0, 0)
    return start_date, end_date, new_end_date.strftime('%Y-%m-%d %H:%M:%S')
# dial forward to the next business day if hour exceeds 24
    elif 16 + hours >= 24:
    end_date = end_date_pre + 1*BDay()
    new_end_date = datetime(end_date.year, end_date.month, end_date.day,9, 0, 0)
    return start_date, end_date, new_end_date.strftime('%Y-%m-%d %H:%M:%S')
    else:
    return start_date, end_date, end_date.strftime('%Y-%m-%d %H:%M:%S')
  • 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. Editorial Team
    Editorial Team
    2026-06-09T10:30:13+00:00Added an answer on June 9, 2026 at 10:30 am

    The dateutil package should be helpful, e.g.

    from dateutil.rrule import *    
    
    def add_weekday_seconds(start, x):
        rr = rrule(SECONDLY, byweekday=(MO, TU, WE, TH, FR), dtstart=start, interval=x)
        return rr.after(start)
    

    This (1) uses the rrule class to creates a “repeating date rule” that includes all seconds within Monday through Friday starting on start (which is a datetime), skipping every x seconds; and then (2) executes this rule using the after method which returns the first datetime matching the the rule after the start time — which should be your answer!

    Tests of adding 5, 10, and 15 seconds to a start time of 10 seconds before midnight on Friday night, resulting in, respectively, 5 seconds before midnight on Friday, midnight Monday morning, and 5 seconds after midnight Monday morning:

    In [131]: friday_night = datetime.datetime(2012, 8, 10, 23, 59, 50)
    
    In [132]: add_weekday_seconds(friday_night, 5)
    Out[132]: datetime.datetime(2012, 8, 10, 23, 59, 55)
    
    In [133]: add_weekday_seconds(friday_night, 10)
    Out[133]: datetime.datetime(2012, 8, 13, 0, 0)
    
    In [134]: add_weekday_seconds(friday_night, 15)
    Out[134]: datetime.datetime(2012, 8, 13, 0, 0, 5)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add three videos on my website. I'm using jw-player. It works,
I am trying add picture boxes dynamically. My code is as PictureBox picture =
Im new to asp.net mvc. I'm trying add new model class but it got
Trying to add a blank sample app for a rails tutorial to GitHub, but
I am trying to add CakePHP's Security Component to my Application, but it slows
I'm trying to implement a iGoogle like dashboard interface using widgets that get their
I'm trying to use the Python profiler to speed up my code. I've been
I'm trying to convert some code from Python to C++ in an effort to
I'm trying to add multithreading to a Python app, and thus started with some
I'm trying to set up argparse on my python program but it's not working.

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.