2

I’m fairly new to Linux and shell scripting so I hope that you will forgive a nooby question.

I want to get the current date but with a fixed time into a variable with the value in epoch seconds.

I have done this so far, but I’m really not happy with it:

startTime=$(date -d $(date +"%FT03:15:00") +%s)

It works ok but just seems a bit clunky (mainly because of the two calls to date) so was wondering if there is a better/cleaner way to achieve what I’m after?

I tried a printf alternative which cut down on one date call but...

The jobs that’s running this isn’t performance critical but I like to code for efficiency out of habit.

Ed Morton
209k18 gold badges90 silver badges212 bronze badges
asked Sep 3, 2023 at 14:49
3
  • 5
    date is smart enough to understand that if you say 03:15:00 without specifying the full date, that means today. So date -d 03:15:00 +%s might be what you want Commented Sep 3, 2023 at 15:06
  • Perfect! Works exactly how I wanted, is more concise and efficient. Many thanks Commented Sep 4, 2023 at 4:57
  • FWIW, it's "newbie" (or just NB), not "nooby". Welcome to StackOverflow. Commented Sep 4, 2023 at 8:56

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.