Operaciones con fechas en python

53 views
Skip to first unread message

Renich Bon Ciric

unread,
Apr 24, 2016, 6:47:01 PM4/24/16
to pytho...@googlegroups.com
Hola,

Ayer, el Sergio me dijo que hacer cosas con fechas en python era muy
fácil. Yo le he batallado mucho.

Como soy un pythonero de bajo nivel y mucho más proclive a ruby, envío
una serie de operaciones que me gustaría poder hacer, de manera
sencilla, en python: y sin andar incluyendo cuanto egg por todos
lados.

#!/usr/bin/env ruby

# date operations
require 'date'
d = DateTime.now

puts '# today - 10 days'
d = d - 10
puts d
puts

puts '# add 2 months'
d = d >> 2
puts d
puts

puts 'parse date'
print 'plese, tell me a date: '
d = gets
puts DateTime.parse( d )
puts

# time operations
puts '# time'
t = Time.now
puts t
puts

puts '# - 10 secs'
t1 = t - 10
puts t
puts

puts '# diff'
puts t - t1


--
It's hard to be free... but I love to struggle. Love isn't asked for;
it's just given. Respect isn't asked for; it's earned!
Renich Bon Ciric

http://www.woralelandia.com/
http://www.introbella.com/

JZA

unread,
Apr 24, 2016, 7:12:25 PM4/24/16
to pythoncabal
Checa el modulo de fechas en python, es parte de las librerias estandares:
https://docs.python.org/3/library/datetime.html
tienes cosas como:
datetime.now(tz=None)
datetime.today()
o
datetime.hour

--
Has recibido este mensaje porque estás suscrito al grupo "PythonCabal" de Grupos de Google.
Para anular la suscripción a este grupo y dejar de recibir sus mensajes, envía un correo electrónico a pythoncabal...@googlegroups.com.
Para publicar una entrada en este grupo, envía un correo electrónico a pytho...@googlegroups.com.
Visita este grupo en https://groups.google.com/group/pythoncabal.
Para obtener más opciones, visita https://groups.google.com/d/optout.



--
Alexandro Colorado
Apache OpenOffice Contributor
9060 55AB FFD2 2F02 0E1A  3409 599C 14FC 9450 D3CF

Sergio Ezequiel Gutierrez Alvarez

unread,
Apr 25, 2016, 3:57:35 AM4/25/16
to pytho...@googlegroups.com
Aquí te dejo la secuencia equivalente a tu ejemplo en ruby:

>>> Import datetime

>>> d = datetime.datetime.now()
>>> d2 = datetime.datetime.now()
>>> d
datetime.datetime(2016, 4, 25, 2, 50, 3, 802579)

>>> d - datetime.timedelta(10)
datetime.datetime(2016, 4, 15, 2, 50, 3, 802579)

>>> d + datetime.timedelta(60)
datetime.datetime(2016, 6, 24, 2, 50, 3, 802579)

>>> d - datetime.timedelta(seconds=10)
datetime.datetime(2016, 4, 25, 2, 49, 53, 802579)

>>> d2 - d
datetime.timedelta(0, 3, 704074)

El delta se esta representado como:
datetime.timedelta(dias, segundos, microsegundos)

Saludos

Para publicar en este grupo, envía un correo electrónico a pytho...@googlegroups.com.
Para acceder a más opciones, visita https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages