45687

datetime: print as seconds

Question:

I have a datetime object. I want to print it as just number of seconds (i.e., 1 min 30.5 sec should print as 90.5 s). Can't seem to find a way to do it with strftime.

Answer1:

I think that for your situation you'd be better off using a datetime.timedelta object. It has a function that will do exactly what you want, datetime.timedelta.total_seconds().

Answer2:

Datetimes are instants in time, unlike <em>durations</em> which are just numbers. The reason strftime won't say "90.5s" is that, well, that is a duration and not a datetime.

Does your datetime object represent the instant that is 90.5 seconds past the epoch? If so, just get the epoch time in millis and divide by 1000, concat an "s" and you are good to go.

Also, for fun, see <a href="http://en.wikipedia.org/wiki/ISO_8601#Durations" rel="nofollow">http://en.wikipedia.org/wiki/ISO_8601#Durations</a>.

Recommend

  • Time difference returning integer ansi sql 92
  • How to convert datetime string to UTC to plot points on Highcharts
  • Converting Pandas DatetimeIndex to a numeric format
  • Django: time zone issue
  • How to compare dates in Python?
  • Entity Framework inserting duplicates on Seeding database [duplicate]
  • Time and date difference in 2 cells in Excel
  • Converting Date/Time to Just Time
  • Linq - Getting calendar events that have Start and Length properties
  • Angular Chart JS - Multiple data sets with combined x axis
  • Populating HTML5 Datepicker With Data Passed From A Model
  • Error (10818): Can't infer register for “E” at clk200Hz.vhd(29) because it does not hold its va
  • AIX equivalent for 'date --date'
  • Apache Spark selects all rows
  • wait function that uses while
  • Python, Bokeh: How to change range of datetime axis
  • Output Ruby duration in iso8601
  • TypeError: unsupported operand type(s) for -: 'str' and 'str' in python 3.x Anac
  • Rails: Correct Way of Using the method of PostController in the Index file
  • Convert ctime to unicode and unicode to ctime python
  • Get Quarters StartDate and EndDate from Year
  • Determine if a weekday is between two dates in R
  • Facebook Messenger Platform - Detect user typing
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • MVC3 Razor - ListBox pre-select not working
  • JavaScriptCore crash on iOS9
  • SVN: Merging two branches together
  • Transpose CSV data with awk (pivot transformation)
  • Acquiring multiple attributes from .xml file in c#
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Is there any way to bind data to data.frame by some index?
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • How can i traverse a binary tree from right to left in java?
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?