Created
July 7, 2012 20:42
-
-
Save yyl/3068026 to your computer and use it in GitHub Desktop.
datetime module in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> datetime.now() | |
| datetime.datetime(2012, 7, 7, 16, 31, 24, 604514) | |
| >>> datetime.today() | |
| datetime.datetime(2012, 7, 7, 16, 31, 27, 340110) | |
| >>> datetime.utcnow() | |
| datetime.datetime(2012, 7, 7, 20, 31, 58, 227201) | |
| >>> d = datetime.strptime("2012-07-07 16:00", "%Y-%m-%d %H:%M") | |
| >>> d | |
| datetime.datetime(2012, 7, 7, 16, 0) | |
| >>> datetime.utcnow().timetuple() | |
| time.struct_time(tm_year=2012, tm_mon=7, tm_mday=7, tm_hour=20, tm_min=47, tm_sec=56, tm_wday=5, tm_yday=189, tm_isdst=-1) | |
| >>> list(datetime.utcnow().timetuple()[0:3]) | |
| [2012, 7, 7] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> datetime.utcnow().date() | |
| datetime.date(2012, 7, 7) | |
| >>> from datetime import timedelta | |
| >>> shift = timedelta(days=100) | |
| >>> day = datetime.utcnow().date() | |
| >>> day + shift | |
| datetime.date(2012, 10, 15) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private String parseTime(long t) { | |
| String format = "yyyy-MM-dd HH:mm"; | |
| SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US); | |
| // DateFormat df = DateFormat.getTimeInstance(DateFormat.MEDIUM); | |
| sdf.setTimeZone(TimeZone.getTimeZone("GMT-4")); | |
| String gmtTime = sdf.format(t); | |
| return gmtTime; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment