What was midnight yesterday as an epoch time?












17















I'm trying to get my head around the datetime module. I know the time now as an epoch and the time an event last happened (as an epoch time). What I need to do is figure out whether that event happened between midnight and midnight of yesterday.



t = time.time() # is now
t2 = 1234567890 # some arbitrary time from my log


24 hours ago is t - 86400, but how can I round that up and down to midnight. I'm having real trouble finding a way to get timestamps in and out of datetime or then manipulating a datetime to set the time.










share|improve this question




















  • 1





    Sounds like you may have to convert it to regular time somewhere in there, since dividers like days/midnight/etc are not a part of epoch times.

    – Gray
    Oct 25 '13 at 16:15
















17















I'm trying to get my head around the datetime module. I know the time now as an epoch and the time an event last happened (as an epoch time). What I need to do is figure out whether that event happened between midnight and midnight of yesterday.



t = time.time() # is now
t2 = 1234567890 # some arbitrary time from my log


24 hours ago is t - 86400, but how can I round that up and down to midnight. I'm having real trouble finding a way to get timestamps in and out of datetime or then manipulating a datetime to set the time.










share|improve this question




















  • 1





    Sounds like you may have to convert it to regular time somewhere in there, since dividers like days/midnight/etc are not a part of epoch times.

    – Gray
    Oct 25 '13 at 16:15














17












17








17


5






I'm trying to get my head around the datetime module. I know the time now as an epoch and the time an event last happened (as an epoch time). What I need to do is figure out whether that event happened between midnight and midnight of yesterday.



t = time.time() # is now
t2 = 1234567890 # some arbitrary time from my log


24 hours ago is t - 86400, but how can I round that up and down to midnight. I'm having real trouble finding a way to get timestamps in and out of datetime or then manipulating a datetime to set the time.










share|improve this question
















I'm trying to get my head around the datetime module. I know the time now as an epoch and the time an event last happened (as an epoch time). What I need to do is figure out whether that event happened between midnight and midnight of yesterday.



t = time.time() # is now
t2 = 1234567890 # some arbitrary time from my log


24 hours ago is t - 86400, but how can I round that up and down to midnight. I'm having real trouble finding a way to get timestamps in and out of datetime or then manipulating a datetime to set the time.







python datetime






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 5 '14 at 7:00









Martijn Pieters

717k13925052319




717k13925052319










asked Oct 25 '13 at 16:13









t0mmytt0mmyt

1831312




1831312








  • 1





    Sounds like you may have to convert it to regular time somewhere in there, since dividers like days/midnight/etc are not a part of epoch times.

    – Gray
    Oct 25 '13 at 16:15














  • 1





    Sounds like you may have to convert it to regular time somewhere in there, since dividers like days/midnight/etc are not a part of epoch times.

    – Gray
    Oct 25 '13 at 16:15








1




1





Sounds like you may have to convert it to regular time somewhere in there, since dividers like days/midnight/etc are not a part of epoch times.

– Gray
Oct 25 '13 at 16:15





Sounds like you may have to convert it to regular time somewhere in there, since dividers like days/midnight/etc are not a part of epoch times.

– Gray
Oct 25 '13 at 16:15












5 Answers
5






active

oldest

votes


















40














In the Middle of the Night



Generating the last midnight is easy:



from datetime import datetime, time

midnight = datetime.combine(datetime.today(), time.min)


That combines today's date (you can use date() or a datetime() instance, your pick), together with time.min to form a datetime object at midnight.



Yesterday



With a timedelta() you can calculate the previous midnight:



from datetime import timedelta

yesterday_midnight = midnight - timedelta(days=1)


That Was Yesterday



Now test if your timestamp is in between these two points:



timestamp = datetime.fromtimestamp(some_timestamp_from_your_log)
if yesterday_midnight <= timestamp < midnight:
# this happened between 00:00:00 and 23:59:59 yesterday


All Together Now



Combined into one function:



from datetime import datetime, time, timedelta

def is_yesterday(timestamp):
midnight = datetime.combine(datetime.today(), time.min)
yesterday_midnight = midnight - timedelta(days=1)
return yesterday_midnight <= timestamp < midnight:

if is_yesterday(datetime.fromtimestamp(some_timestamp_from_your_log)):
# ...





share|improve this answer


























  • How can you tell when midnight is without specifying the timezone?

    – sdanzig
    Oct 25 '13 at 16:28











  • @sdanzig: By default, the datetime module is not timezone aware. It can return local or UTC times, as provided by the OS. This code uses local time.

    – Martijn Pieters
    Oct 25 '13 at 16:29








  • 1





    In this case it's not an issue as everything runs in UTC.

    – t0mmyt
    Oct 25 '13 at 16:30








  • 1





    If everything runs UTC, then not only is it fine, but there's a smart developer behind it. Just to be aware though, the concept of "yesterday" will only apply to what England considers yesterday. If you wanted to do something based on the timing at the site where the logs were written, for instance, you'd have to specify the timezone accordingly.

    – sdanzig
    Oct 25 '13 at 16:34



















2














Given such a timestamp, you can use divmod to compute the number of days since the epoch (which you don't care about), and how many seconds are leftover (which you do):



days_since, remaining_seconds = divmod(t, 24*3600)  # Divide by number of seconds in one day


Then, you subtract the leftover seconds from your original timestamp, which produces midnight
of the current day.



t -= remaining_seconds


Rounding up is as simple as shifting your target timestamp forward exactly one day before rounding down.



tomorrow_t = t + 24 * 3600
days_since, remaining_seconds = divmod(tomorrow_t, 24*3600)
t = tomorrow_t - remaining_seconds





share|improve this answer
























  • I thought about that approach, I was actually just going to mod 86400, but then I remembered leap seconds.

    – t0mmyt
    Oct 25 '13 at 16:45






  • 1





    These timestamps are not affected by leap seconds. When a leap second occurs, the UNIX epoch timestamp simply repeats, so you are guaranteed to have 86400 seconds per day. Repeating a timestamp is just another arbitrary label for the leap second, like 23:59:60 is an arbitrary label for the leap second between 23:59:59 and 00:00:00 (or whenever leap seconds get applied).

    – chepner
    Oct 25 '13 at 17:02





















0














In my estimation, many date and time manipulations are easier to do, and to understand, using the arrow library. This is one of them.



Create an arbitrary date and time.



>>> import arrow
>>> arbitrary = arrow.get(2017,8,16,11,5)


Calculate midnight_yesterday: first, midnight of arbitrary as its 'day' floor; then shift this back by one day. Display the result.



>>> midnight_yesterday = arbitrary.floor('day').shift(days=-1)
>>> midnight_yesterday
<Arrow [2017-08-15T00:00:00+00:00]>


Use timestamp for the desired overall result, for Python 3.3+.



>>> midnight_yesterday.datetime.timestamp()
1502755200.0


Or use this expression for Python 2.7. (Credit: https://stackoverflow.com/a/11743262/131187 for the latter two expressions.)



>>> (midnight_yesterday-arrow.get(1970,1,1)).total_seconds()
1502755200.0





share|improve this answer































    0














    You can use this code:



    import time

    seconds_of_day = 24 * 60 * 60 # 86400
    last_midnight = (round(time.time()) // seconds_of_day) * seconds_of_day
    yesterday_last_midnight = last_midnight - seconds_of_day





    share|improve this answer































      0














      Midnight at the start of today is:



      midnight = (int(time.time() // 86400)) * 86400


      so yesterday's midnight is:



      midnight = (int(time.time() // 86400)) * 86400 - 86400





      share|improve this answer


























      • You must use // instead of %

        – Mastisa
        Jan 1 at 10:10











      • Mastisa - many thanks for the correction; I was about to make it myself.

        – ggemmill
        Jan 1 at 16:34











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19594747%2fwhat-was-midnight-yesterday-as-an-epoch-time%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      40














      In the Middle of the Night



      Generating the last midnight is easy:



      from datetime import datetime, time

      midnight = datetime.combine(datetime.today(), time.min)


      That combines today's date (you can use date() or a datetime() instance, your pick), together with time.min to form a datetime object at midnight.



      Yesterday



      With a timedelta() you can calculate the previous midnight:



      from datetime import timedelta

      yesterday_midnight = midnight - timedelta(days=1)


      That Was Yesterday



      Now test if your timestamp is in between these two points:



      timestamp = datetime.fromtimestamp(some_timestamp_from_your_log)
      if yesterday_midnight <= timestamp < midnight:
      # this happened between 00:00:00 and 23:59:59 yesterday


      All Together Now



      Combined into one function:



      from datetime import datetime, time, timedelta

      def is_yesterday(timestamp):
      midnight = datetime.combine(datetime.today(), time.min)
      yesterday_midnight = midnight - timedelta(days=1)
      return yesterday_midnight <= timestamp < midnight:

      if is_yesterday(datetime.fromtimestamp(some_timestamp_from_your_log)):
      # ...





      share|improve this answer


























      • How can you tell when midnight is without specifying the timezone?

        – sdanzig
        Oct 25 '13 at 16:28











      • @sdanzig: By default, the datetime module is not timezone aware. It can return local or UTC times, as provided by the OS. This code uses local time.

        – Martijn Pieters
        Oct 25 '13 at 16:29








      • 1





        In this case it's not an issue as everything runs in UTC.

        – t0mmyt
        Oct 25 '13 at 16:30








      • 1





        If everything runs UTC, then not only is it fine, but there's a smart developer behind it. Just to be aware though, the concept of "yesterday" will only apply to what England considers yesterday. If you wanted to do something based on the timing at the site where the logs were written, for instance, you'd have to specify the timezone accordingly.

        – sdanzig
        Oct 25 '13 at 16:34
















      40














      In the Middle of the Night



      Generating the last midnight is easy:



      from datetime import datetime, time

      midnight = datetime.combine(datetime.today(), time.min)


      That combines today's date (you can use date() or a datetime() instance, your pick), together with time.min to form a datetime object at midnight.



      Yesterday



      With a timedelta() you can calculate the previous midnight:



      from datetime import timedelta

      yesterday_midnight = midnight - timedelta(days=1)


      That Was Yesterday



      Now test if your timestamp is in between these two points:



      timestamp = datetime.fromtimestamp(some_timestamp_from_your_log)
      if yesterday_midnight <= timestamp < midnight:
      # this happened between 00:00:00 and 23:59:59 yesterday


      All Together Now



      Combined into one function:



      from datetime import datetime, time, timedelta

      def is_yesterday(timestamp):
      midnight = datetime.combine(datetime.today(), time.min)
      yesterday_midnight = midnight - timedelta(days=1)
      return yesterday_midnight <= timestamp < midnight:

      if is_yesterday(datetime.fromtimestamp(some_timestamp_from_your_log)):
      # ...





      share|improve this answer


























      • How can you tell when midnight is without specifying the timezone?

        – sdanzig
        Oct 25 '13 at 16:28











      • @sdanzig: By default, the datetime module is not timezone aware. It can return local or UTC times, as provided by the OS. This code uses local time.

        – Martijn Pieters
        Oct 25 '13 at 16:29








      • 1





        In this case it's not an issue as everything runs in UTC.

        – t0mmyt
        Oct 25 '13 at 16:30








      • 1





        If everything runs UTC, then not only is it fine, but there's a smart developer behind it. Just to be aware though, the concept of "yesterday" will only apply to what England considers yesterday. If you wanted to do something based on the timing at the site where the logs were written, for instance, you'd have to specify the timezone accordingly.

        – sdanzig
        Oct 25 '13 at 16:34














      40












      40








      40







      In the Middle of the Night



      Generating the last midnight is easy:



      from datetime import datetime, time

      midnight = datetime.combine(datetime.today(), time.min)


      That combines today's date (you can use date() or a datetime() instance, your pick), together with time.min to form a datetime object at midnight.



      Yesterday



      With a timedelta() you can calculate the previous midnight:



      from datetime import timedelta

      yesterday_midnight = midnight - timedelta(days=1)


      That Was Yesterday



      Now test if your timestamp is in between these two points:



      timestamp = datetime.fromtimestamp(some_timestamp_from_your_log)
      if yesterday_midnight <= timestamp < midnight:
      # this happened between 00:00:00 and 23:59:59 yesterday


      All Together Now



      Combined into one function:



      from datetime import datetime, time, timedelta

      def is_yesterday(timestamp):
      midnight = datetime.combine(datetime.today(), time.min)
      yesterday_midnight = midnight - timedelta(days=1)
      return yesterday_midnight <= timestamp < midnight:

      if is_yesterday(datetime.fromtimestamp(some_timestamp_from_your_log)):
      # ...





      share|improve this answer















      In the Middle of the Night



      Generating the last midnight is easy:



      from datetime import datetime, time

      midnight = datetime.combine(datetime.today(), time.min)


      That combines today's date (you can use date() or a datetime() instance, your pick), together with time.min to form a datetime object at midnight.



      Yesterday



      With a timedelta() you can calculate the previous midnight:



      from datetime import timedelta

      yesterday_midnight = midnight - timedelta(days=1)


      That Was Yesterday



      Now test if your timestamp is in between these two points:



      timestamp = datetime.fromtimestamp(some_timestamp_from_your_log)
      if yesterday_midnight <= timestamp < midnight:
      # this happened between 00:00:00 and 23:59:59 yesterday


      All Together Now



      Combined into one function:



      from datetime import datetime, time, timedelta

      def is_yesterday(timestamp):
      midnight = datetime.combine(datetime.today(), time.min)
      yesterday_midnight = midnight - timedelta(days=1)
      return yesterday_midnight <= timestamp < midnight:

      if is_yesterday(datetime.fromtimestamp(some_timestamp_from_your_log)):
      # ...






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 29 '18 at 17:48

























      answered Oct 25 '13 at 16:21









      Martijn PietersMartijn Pieters

      717k13925052319




      717k13925052319













      • How can you tell when midnight is without specifying the timezone?

        – sdanzig
        Oct 25 '13 at 16:28











      • @sdanzig: By default, the datetime module is not timezone aware. It can return local or UTC times, as provided by the OS. This code uses local time.

        – Martijn Pieters
        Oct 25 '13 at 16:29








      • 1





        In this case it's not an issue as everything runs in UTC.

        – t0mmyt
        Oct 25 '13 at 16:30








      • 1





        If everything runs UTC, then not only is it fine, but there's a smart developer behind it. Just to be aware though, the concept of "yesterday" will only apply to what England considers yesterday. If you wanted to do something based on the timing at the site where the logs were written, for instance, you'd have to specify the timezone accordingly.

        – sdanzig
        Oct 25 '13 at 16:34



















      • How can you tell when midnight is without specifying the timezone?

        – sdanzig
        Oct 25 '13 at 16:28











      • @sdanzig: By default, the datetime module is not timezone aware. It can return local or UTC times, as provided by the OS. This code uses local time.

        – Martijn Pieters
        Oct 25 '13 at 16:29








      • 1





        In this case it's not an issue as everything runs in UTC.

        – t0mmyt
        Oct 25 '13 at 16:30








      • 1





        If everything runs UTC, then not only is it fine, but there's a smart developer behind it. Just to be aware though, the concept of "yesterday" will only apply to what England considers yesterday. If you wanted to do something based on the timing at the site where the logs were written, for instance, you'd have to specify the timezone accordingly.

        – sdanzig
        Oct 25 '13 at 16:34

















      How can you tell when midnight is without specifying the timezone?

      – sdanzig
      Oct 25 '13 at 16:28





      How can you tell when midnight is without specifying the timezone?

      – sdanzig
      Oct 25 '13 at 16:28













      @sdanzig: By default, the datetime module is not timezone aware. It can return local or UTC times, as provided by the OS. This code uses local time.

      – Martijn Pieters
      Oct 25 '13 at 16:29







      @sdanzig: By default, the datetime module is not timezone aware. It can return local or UTC times, as provided by the OS. This code uses local time.

      – Martijn Pieters
      Oct 25 '13 at 16:29






      1




      1





      In this case it's not an issue as everything runs in UTC.

      – t0mmyt
      Oct 25 '13 at 16:30







      In this case it's not an issue as everything runs in UTC.

      – t0mmyt
      Oct 25 '13 at 16:30






      1




      1





      If everything runs UTC, then not only is it fine, but there's a smart developer behind it. Just to be aware though, the concept of "yesterday" will only apply to what England considers yesterday. If you wanted to do something based on the timing at the site where the logs were written, for instance, you'd have to specify the timezone accordingly.

      – sdanzig
      Oct 25 '13 at 16:34





      If everything runs UTC, then not only is it fine, but there's a smart developer behind it. Just to be aware though, the concept of "yesterday" will only apply to what England considers yesterday. If you wanted to do something based on the timing at the site where the logs were written, for instance, you'd have to specify the timezone accordingly.

      – sdanzig
      Oct 25 '13 at 16:34













      2














      Given such a timestamp, you can use divmod to compute the number of days since the epoch (which you don't care about), and how many seconds are leftover (which you do):



      days_since, remaining_seconds = divmod(t, 24*3600)  # Divide by number of seconds in one day


      Then, you subtract the leftover seconds from your original timestamp, which produces midnight
      of the current day.



      t -= remaining_seconds


      Rounding up is as simple as shifting your target timestamp forward exactly one day before rounding down.



      tomorrow_t = t + 24 * 3600
      days_since, remaining_seconds = divmod(tomorrow_t, 24*3600)
      t = tomorrow_t - remaining_seconds





      share|improve this answer
























      • I thought about that approach, I was actually just going to mod 86400, but then I remembered leap seconds.

        – t0mmyt
        Oct 25 '13 at 16:45






      • 1





        These timestamps are not affected by leap seconds. When a leap second occurs, the UNIX epoch timestamp simply repeats, so you are guaranteed to have 86400 seconds per day. Repeating a timestamp is just another arbitrary label for the leap second, like 23:59:60 is an arbitrary label for the leap second between 23:59:59 and 00:00:00 (or whenever leap seconds get applied).

        – chepner
        Oct 25 '13 at 17:02


















      2














      Given such a timestamp, you can use divmod to compute the number of days since the epoch (which you don't care about), and how many seconds are leftover (which you do):



      days_since, remaining_seconds = divmod(t, 24*3600)  # Divide by number of seconds in one day


      Then, you subtract the leftover seconds from your original timestamp, which produces midnight
      of the current day.



      t -= remaining_seconds


      Rounding up is as simple as shifting your target timestamp forward exactly one day before rounding down.



      tomorrow_t = t + 24 * 3600
      days_since, remaining_seconds = divmod(tomorrow_t, 24*3600)
      t = tomorrow_t - remaining_seconds





      share|improve this answer
























      • I thought about that approach, I was actually just going to mod 86400, but then I remembered leap seconds.

        – t0mmyt
        Oct 25 '13 at 16:45






      • 1





        These timestamps are not affected by leap seconds. When a leap second occurs, the UNIX epoch timestamp simply repeats, so you are guaranteed to have 86400 seconds per day. Repeating a timestamp is just another arbitrary label for the leap second, like 23:59:60 is an arbitrary label for the leap second between 23:59:59 and 00:00:00 (or whenever leap seconds get applied).

        – chepner
        Oct 25 '13 at 17:02
















      2












      2








      2







      Given such a timestamp, you can use divmod to compute the number of days since the epoch (which you don't care about), and how many seconds are leftover (which you do):



      days_since, remaining_seconds = divmod(t, 24*3600)  # Divide by number of seconds in one day


      Then, you subtract the leftover seconds from your original timestamp, which produces midnight
      of the current day.



      t -= remaining_seconds


      Rounding up is as simple as shifting your target timestamp forward exactly one day before rounding down.



      tomorrow_t = t + 24 * 3600
      days_since, remaining_seconds = divmod(tomorrow_t, 24*3600)
      t = tomorrow_t - remaining_seconds





      share|improve this answer













      Given such a timestamp, you can use divmod to compute the number of days since the epoch (which you don't care about), and how many seconds are leftover (which you do):



      days_since, remaining_seconds = divmod(t, 24*3600)  # Divide by number of seconds in one day


      Then, you subtract the leftover seconds from your original timestamp, which produces midnight
      of the current day.



      t -= remaining_seconds


      Rounding up is as simple as shifting your target timestamp forward exactly one day before rounding down.



      tomorrow_t = t + 24 * 3600
      days_since, remaining_seconds = divmod(tomorrow_t, 24*3600)
      t = tomorrow_t - remaining_seconds






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Oct 25 '13 at 16:36









      chepnerchepner

      255k34243337




      255k34243337













      • I thought about that approach, I was actually just going to mod 86400, but then I remembered leap seconds.

        – t0mmyt
        Oct 25 '13 at 16:45






      • 1





        These timestamps are not affected by leap seconds. When a leap second occurs, the UNIX epoch timestamp simply repeats, so you are guaranteed to have 86400 seconds per day. Repeating a timestamp is just another arbitrary label for the leap second, like 23:59:60 is an arbitrary label for the leap second between 23:59:59 and 00:00:00 (or whenever leap seconds get applied).

        – chepner
        Oct 25 '13 at 17:02





















      • I thought about that approach, I was actually just going to mod 86400, but then I remembered leap seconds.

        – t0mmyt
        Oct 25 '13 at 16:45






      • 1





        These timestamps are not affected by leap seconds. When a leap second occurs, the UNIX epoch timestamp simply repeats, so you are guaranteed to have 86400 seconds per day. Repeating a timestamp is just another arbitrary label for the leap second, like 23:59:60 is an arbitrary label for the leap second between 23:59:59 and 00:00:00 (or whenever leap seconds get applied).

        – chepner
        Oct 25 '13 at 17:02



















      I thought about that approach, I was actually just going to mod 86400, but then I remembered leap seconds.

      – t0mmyt
      Oct 25 '13 at 16:45





      I thought about that approach, I was actually just going to mod 86400, but then I remembered leap seconds.

      – t0mmyt
      Oct 25 '13 at 16:45




      1




      1





      These timestamps are not affected by leap seconds. When a leap second occurs, the UNIX epoch timestamp simply repeats, so you are guaranteed to have 86400 seconds per day. Repeating a timestamp is just another arbitrary label for the leap second, like 23:59:60 is an arbitrary label for the leap second between 23:59:59 and 00:00:00 (or whenever leap seconds get applied).

      – chepner
      Oct 25 '13 at 17:02







      These timestamps are not affected by leap seconds. When a leap second occurs, the UNIX epoch timestamp simply repeats, so you are guaranteed to have 86400 seconds per day. Repeating a timestamp is just another arbitrary label for the leap second, like 23:59:60 is an arbitrary label for the leap second between 23:59:59 and 00:00:00 (or whenever leap seconds get applied).

      – chepner
      Oct 25 '13 at 17:02













      0














      In my estimation, many date and time manipulations are easier to do, and to understand, using the arrow library. This is one of them.



      Create an arbitrary date and time.



      >>> import arrow
      >>> arbitrary = arrow.get(2017,8,16,11,5)


      Calculate midnight_yesterday: first, midnight of arbitrary as its 'day' floor; then shift this back by one day. Display the result.



      >>> midnight_yesterday = arbitrary.floor('day').shift(days=-1)
      >>> midnight_yesterday
      <Arrow [2017-08-15T00:00:00+00:00]>


      Use timestamp for the desired overall result, for Python 3.3+.



      >>> midnight_yesterday.datetime.timestamp()
      1502755200.0


      Or use this expression for Python 2.7. (Credit: https://stackoverflow.com/a/11743262/131187 for the latter two expressions.)



      >>> (midnight_yesterday-arrow.get(1970,1,1)).total_seconds()
      1502755200.0





      share|improve this answer




























        0














        In my estimation, many date and time manipulations are easier to do, and to understand, using the arrow library. This is one of them.



        Create an arbitrary date and time.



        >>> import arrow
        >>> arbitrary = arrow.get(2017,8,16,11,5)


        Calculate midnight_yesterday: first, midnight of arbitrary as its 'day' floor; then shift this back by one day. Display the result.



        >>> midnight_yesterday = arbitrary.floor('day').shift(days=-1)
        >>> midnight_yesterday
        <Arrow [2017-08-15T00:00:00+00:00]>


        Use timestamp for the desired overall result, for Python 3.3+.



        >>> midnight_yesterday.datetime.timestamp()
        1502755200.0


        Or use this expression for Python 2.7. (Credit: https://stackoverflow.com/a/11743262/131187 for the latter two expressions.)



        >>> (midnight_yesterday-arrow.get(1970,1,1)).total_seconds()
        1502755200.0





        share|improve this answer


























          0












          0








          0







          In my estimation, many date and time manipulations are easier to do, and to understand, using the arrow library. This is one of them.



          Create an arbitrary date and time.



          >>> import arrow
          >>> arbitrary = arrow.get(2017,8,16,11,5)


          Calculate midnight_yesterday: first, midnight of arbitrary as its 'day' floor; then shift this back by one day. Display the result.



          >>> midnight_yesterday = arbitrary.floor('day').shift(days=-1)
          >>> midnight_yesterday
          <Arrow [2017-08-15T00:00:00+00:00]>


          Use timestamp for the desired overall result, for Python 3.3+.



          >>> midnight_yesterday.datetime.timestamp()
          1502755200.0


          Or use this expression for Python 2.7. (Credit: https://stackoverflow.com/a/11743262/131187 for the latter two expressions.)



          >>> (midnight_yesterday-arrow.get(1970,1,1)).total_seconds()
          1502755200.0





          share|improve this answer













          In my estimation, many date and time manipulations are easier to do, and to understand, using the arrow library. This is one of them.



          Create an arbitrary date and time.



          >>> import arrow
          >>> arbitrary = arrow.get(2017,8,16,11,5)


          Calculate midnight_yesterday: first, midnight of arbitrary as its 'day' floor; then shift this back by one day. Display the result.



          >>> midnight_yesterday = arbitrary.floor('day').shift(days=-1)
          >>> midnight_yesterday
          <Arrow [2017-08-15T00:00:00+00:00]>


          Use timestamp for the desired overall result, for Python 3.3+.



          >>> midnight_yesterday.datetime.timestamp()
          1502755200.0


          Or use this expression for Python 2.7. (Credit: https://stackoverflow.com/a/11743262/131187 for the latter two expressions.)



          >>> (midnight_yesterday-arrow.get(1970,1,1)).total_seconds()
          1502755200.0






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 29 '17 at 16:29









          Bill BellBill Bell

          15.7k42642




          15.7k42642























              0














              You can use this code:



              import time

              seconds_of_day = 24 * 60 * 60 # 86400
              last_midnight = (round(time.time()) // seconds_of_day) * seconds_of_day
              yesterday_last_midnight = last_midnight - seconds_of_day





              share|improve this answer




























                0














                You can use this code:



                import time

                seconds_of_day = 24 * 60 * 60 # 86400
                last_midnight = (round(time.time()) // seconds_of_day) * seconds_of_day
                yesterday_last_midnight = last_midnight - seconds_of_day





                share|improve this answer


























                  0












                  0








                  0







                  You can use this code:



                  import time

                  seconds_of_day = 24 * 60 * 60 # 86400
                  last_midnight = (round(time.time()) // seconds_of_day) * seconds_of_day
                  yesterday_last_midnight = last_midnight - seconds_of_day





                  share|improve this answer













                  You can use this code:



                  import time

                  seconds_of_day = 24 * 60 * 60 # 86400
                  last_midnight = (round(time.time()) // seconds_of_day) * seconds_of_day
                  yesterday_last_midnight = last_midnight - seconds_of_day






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 1 at 10:10









                  MastisaMastisa

                  1,0962922




                  1,0962922























                      0














                      Midnight at the start of today is:



                      midnight = (int(time.time() // 86400)) * 86400


                      so yesterday's midnight is:



                      midnight = (int(time.time() // 86400)) * 86400 - 86400





                      share|improve this answer


























                      • You must use // instead of %

                        – Mastisa
                        Jan 1 at 10:10











                      • Mastisa - many thanks for the correction; I was about to make it myself.

                        – ggemmill
                        Jan 1 at 16:34
















                      0














                      Midnight at the start of today is:



                      midnight = (int(time.time() // 86400)) * 86400


                      so yesterday's midnight is:



                      midnight = (int(time.time() // 86400)) * 86400 - 86400





                      share|improve this answer


























                      • You must use // instead of %

                        – Mastisa
                        Jan 1 at 10:10











                      • Mastisa - many thanks for the correction; I was about to make it myself.

                        – ggemmill
                        Jan 1 at 16:34














                      0












                      0








                      0







                      Midnight at the start of today is:



                      midnight = (int(time.time() // 86400)) * 86400


                      so yesterday's midnight is:



                      midnight = (int(time.time() // 86400)) * 86400 - 86400





                      share|improve this answer















                      Midnight at the start of today is:



                      midnight = (int(time.time() // 86400)) * 86400


                      so yesterday's midnight is:



                      midnight = (int(time.time() // 86400)) * 86400 - 86400






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jan 2 at 5:50









                      Mastisa

                      1,0962922




                      1,0962922










                      answered Dec 30 '18 at 17:36









                      ggemmillggemmill

                      44




                      44













                      • You must use // instead of %

                        – Mastisa
                        Jan 1 at 10:10











                      • Mastisa - many thanks for the correction; I was about to make it myself.

                        – ggemmill
                        Jan 1 at 16:34



















                      • You must use // instead of %

                        – Mastisa
                        Jan 1 at 10:10











                      • Mastisa - many thanks for the correction; I was about to make it myself.

                        – ggemmill
                        Jan 1 at 16:34

















                      You must use // instead of %

                      – Mastisa
                      Jan 1 at 10:10





                      You must use // instead of %

                      – Mastisa
                      Jan 1 at 10:10













                      Mastisa - many thanks for the correction; I was about to make it myself.

                      – ggemmill
                      Jan 1 at 16:34





                      Mastisa - many thanks for the correction; I was about to make it myself.

                      – ggemmill
                      Jan 1 at 16:34


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19594747%2fwhat-was-midnight-yesterday-as-an-epoch-time%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                      ts Property 'filter' does not exist on type '{}'

                      mat-slide-toggle shouldn't change it's state when I click cancel in confirmation window