Python gets local time/date formatting

Source: Internet
Author: User
Tags local time month name
Get current time:

Python code

Import Time            print time.time () #获取当前时间           print time.strftime ("%y-%m-%d  %x") #获取当前时间 (in specified format)

Using the Time.localtime () method, the function is to format the timestamp as local time:

Python code

Import  time  print Time.localtime (Time.time ())

Output: Time.struct_time (tm_year=2015, tm_mon=11, tm_mday=513, tm_hour=15, tm_min=48, tm_sec=28, tm_wday=3, tm_yday=309, tm_isdst=0)

Now it looks more hopeful that the format will be the time we want.
Time.strftime ('%y-%m-%d ', Time.localtime (Time.time ()))

Python code

Import Time  print time.strftime ('%y-%m-%d ', Time.localtime (Time.time ()))

Finally, using the Time.strftime () method to format just a bunch of information into what we want, now the result is: 2015-11-05

There are a lot of parameters in the Ime.strftime that will allow you to freely output what you want:
The following are the parameters of Time.strftime:

Strftime (format[, tuple]), string
Outputs the specified Struct_time (default current time), based on the specified formatted string

1,python Time Date formatting symbols:

%y Two-digit year representation (00-99)
%Y Four-digit year representation (000-9999)
%m Month (01-12)
One day in%d months (0-31)
%H 24-hour hours (0-23)
%I 12-hour hours (01-12)
%M minutes (00=59)
%s seconds (00-59)
%a Local Simplified Week name
%A Local Full week name
%b a locally simplified month name
%B Local Full month name
%c Local corresponding date representation and time representation
%j Day of the Year (001-366)
%p the equivalent of a local a.m. or p.m.
%u weeks of the year (00-53) Sunday is the beginning of the week
%w Week (0-6), Sunday for the beginning of the week
%W Week of the Year (00-53) Monday is the beginning of the week
%x Local corresponding date representation
%x Local corresponding time representation
%Z the name of the current time zone
Percent% of the number itself

2. Calculate the time difference:

Python code

#coding =utf-8          Import time      import datetime         D1 = datetime.datetime (2013,6,9)      D2 = Datetime.datetime ( 11,5)            print (D1-D2). Days

Output is: 879

3. Calculate the operating time difference:

Python code

#coding =utf-8      <pre>      Import time      import datetime            starttime = Datetime.datetime.now ()            Time.sleep (3)            endtime = Datetime.datetime.now ()      print (endtime-starttime). Seconds <strong> </ Strong>

Output is: 3

4, calculate the date time after 10 days:

Python code

#coding =utf-8            Import time      import datetime            D1 = Datetime.datetime.now ()      d3 = D1 + Datetime.timedelta ( Days =10)            print str (d3)      print d3.ctime () <strong>  </strong>

Output Result:

2015-11-15 14:44:04.291000
Sun Nov 15 14:44:04 2015

5, Gregorian calendar

#! -*-ENCODING:GBK-*-Import re import math import time import os month_name = ["", "January", "F  Ebruary "," March "," April "," may "," June "," July "," August "," September "," October "," November "," December "] Month_days         = [0,31,28,31,30,31,30,31,31,30,31,30,31]; Lunar_calendar_table = [0x04ae53,0x0a5748,0x5526bd,0x0d2650,0x0d9544,0x46aab9,0x056a4d,0x09ad42,0x24aeb6, 0X04AE4A, #//*1901-1910*/0X6A4DBE,0X0A4D52,0X0D2546,0X5D52BA,0X0B544E,0X0D6A43,0X296D37,0X095B4B,0X749BC1, 0x049754, #//*1911-1920*/0x0a4b48,0x5b25bc,0x06a550,0x06d445,0x4adab8,0x02b64d,0x095742,0x2497b7,0x04974a, 0x664b3e, #//*1921-1930*/0x0d4a51,0x0ea546,0x56d4ba,0x05ad4e,0x02b644,0x393738,0x092e4b,0x7c96bf,0x0c9553, 0x0d4a48, #//*1931-1940*/0x6da53b,0x0b554f,0x056a45,0x4aadb9,0x025d4d,0x092d42,0x2c95b6,0x0a954a,0x7b4abd,0x06 CA51, #//*1941-1950*/0x0b5546,0x555abb,0x04da4e,0x0a5b43,0x352bb8,0x052b4c,0x8a953f,0x0e9552,0x06aa48,0x6ad53c,#//*1951-1960*/0x0ab54f,0x04b645,0x4a5739,0x0a574d,0x052642,0x3e9335,0x0d9549,0x75aabe,0x056a51,0x096d46, #//*1 961-1970*/0x54aebb,0x04ad4f,0x0a4d43,0x4d26b7,0x0d254b,0x8d52bf,0x0b5452,0x0b6a47,0x696d3c,0x095b50, #//* 1971-1980*/0X049B45,0X4A4BB9,0X0A4B4D,0XAB25C2,0X06A554,0X06D449,0X6ADA3D,0X0AB651,0X093746,0X5497BB, #//* 1981-1990*/0x04974f,0x064b44,0x36a537,0x0ea54a,0x86b2bf,0x05ac53,0x0ab647,0x5936bc,0x092e50,0x0c9645, #//* 1991-2000*/0x4d4ab8,0x0d4a4c,0x0da541,0x25aab6,0x056a49,0x7aadbd,0x025d52,0x092d47,0x5c95ba,0x0a954e, #//* 2001-2010*/0X0B4A43,0X4B5537,0X0AD54A,0X955ABF,0X04BA53,0X0A5B48,0X652BBC,0X052B50,0X0A9345,0X474AB9, #//* 2011-2020*/0x06aa4c,0x0ad541,0x24dab6,0x04b64a,0x69573d,0x0a4e51,0x0d2646,0x5e933a,0x0d534d,0x05aa43, #//* 2021-2030*/0x36b537,0x096d4b,0xb4aebf,0x04ad53,0x0a4d48,0x6d25bc,0x0d254f,0x0d5244,0x5daa38,0x0b5a4c, #//* 2031-2040*/0x056d41,0x24adb6,0x049b4a,0x7a4bbe,0x0a4b51,0x0aa546,0x5b52ba,0x06d24e,0x0ada42,0x355b37, #//*2041-2050*/0x09374b,0x8497c1,0x049753,0x064b48,0x66a53c,0x0ea54f, 0x06b244,0x4ab638,0x0aae4c,0x092e42, #//*2051-2060*/0x3c9735,0x0c9649,0x7d4abd,0x0d4a51,0x0da545,0x55aaba, 0x056a4e,0x0a6d43,0x452eb7,0x052d4b, #//*2061-2070*/0x8a95bf,0x0a9553,0x0b4a47,0x6b553b,0x0ad54f,0x055a45, 0X4A5D38,0X0A5B4C,0X052B42,0X3A93B6, #//*2071-2080*/0x069349,0x7729bd,0x06aa51,0x0ad546,0x54daba,0x04b64e, 0x0a5743,0x452738,0x0d264a,0x8e933e, #//*2081-2090*/0X0D5252,0X0DAA47,0X66B53B,0X056D4F,0X04AE45,0X4A4EB9, 0X0A4D4C,0X0D1541,0X2D92B5 #//*2091-2099*/] # The following three tables are the structure of the Lunar data table lunarcalendartable.    A total of 32-bit integers are used in the 0~23 bit.   # # 6 5 4 3 2 1 0 # indicates that the Gregorian calendar month of the Spring Festival represents the Gregorian calendar date # # 19 18 17 16 15 14 13 12 11 10 9 8 7 # 1 2 3 4 5 6 7 8 9 10 11 12 13 # Lunar January-1 March size.         The month corresponding bit is 1, the lunar month is large (30 days), 0 is small (29 days) # # 23 22 21 20 # Indicates that the month of the current year, the value of 0 is the year without a leap month.    Def get_month_days (year, month):  Global month_days;          if (month==2): if (((year%4 = = 0) and (year%100! = 0)) or (year%400 = = 0)): Return to Else:         Return Else:return (Month_days[month]); def get_syear_days (syear): if (((syear%4 = = 0) and (syear%100! = 0)) or (syear%400 = 0)): return 366 Else      : Return 365 def get_days_of_syear (Syear, Smonth, Sday): "" "get given day's number of sun year" "         Days = 0 for I in range (1, smonth): Days + = Get_month_days (syear, i) days + = Sday return days def get_days_of_lyear (Syear, Smonth, Sday): "" "get given day's number of the lunar year" "" Global Lunar_c Alendar_table lyear = syear Spring_month = (lunar_calendar_table[syear-1901] & 0x60) >> 5 spring_ Day = (lunar_calendar_table[syear-1901] & 0x1F) if ((Spring_month > Smonth) or ((Spring_month = = Smonth) and ( Spring_day > Sday)): # The day is before SprinG Festival Day, and was previous day in lunar year Spring_month = (Lunar_calendar_table[syear-1901-1] & 0x60) >> 5 Spring_day = (Lunar_calendar_table[syear-1901-1] & 0x1F) Lyear-= 1 Lunar_days = Get _syear_days (lyear) + get_days_of_syear (syear, smonth, sday) \-Get_days_of_syear (Lyear, Spring_month, Spring_d ay) else:lunar_days = Get_days_of_syear (Syear, smonth, sday) \-Get_days_of_syear (Syear, Spring _month, spring_day) lunar_days + = 1 # Consider current day return (Lyear, lunar_days) def get_lunar_date (Syear, Smonth, sday): If Syear < 1901 or syear > 2099:return # Lunar Year, lunar days to spring      Festival lyear, lunar_days = Get_days_of_lyear (Syear, Smonth, Sday);      L_double_month = (lunar_calendar_table[lyear-1901] >>) & 0xF lmonth = Lday = 1 bits = 19 Month_begin_day = 0 for Lmonth in range (1, +): L_montH_big = (lunar_calendar_table[lyear-1901] >> bits) & 0x1 if Month_begin_day + + L_month_big < LUNAR _days:lmonth + = 1 Month_begin_day + + l_month_big Else:lday = Lunar_days-month_b Egin_day Break Bits-= 1 if l_double_month: # Lunar Double month Adjust if L_double_mon           th = = Lmonth-1: Lmonth-= 1 Lmonth + = + # double month Elif L_double_month < lmonth-1:        Lmonth-= 1 return (lyear, Lmonth, lday) if __name__ = = "__main__": Y,m,d = 2010, 9, 28 Print "Sun Calendar 2010-9-28 = = Lunar Calendar", Get_lunar_date (Y,m,d)

Output Result:

Sun Calendar 2010-9-28 = = Lunar Calendar (2010, 8, 21)

The python version of the lunar calendar:

#coding =utf-8 #参见: http://download.csdn.net/source/1178 #****************************************************    # below for the lunar calendar to calculate the required data, in order to save storage space, so use the following more abnormal storage methods. #****************************************************************************** #数组g_lunar_month_ Day is deposited on the lunar calendar from 1901 to 2050 each year, #阴历每月只能是29或30天, 12 (or 13) bits a year, the corresponding bit is 1 table 30 days, otherwise 29 days g_lunar_month_day = [0x4ae0, 0xa570, 0x5268, 0xd260, 0xd950, 0x6aa8, 0x56a0, 0x9ad0, 0x4ae8, 0x4ae0, #1910 0xa4d8, 0xa4d0, 0xd250, 0xd548, 0xb 550, 0x56a0, 0x96d0, 0x95b0, 0x49b8, 0x49b0, #1920 0xa4b0, 0xb258, 0x6a50, 0X6D40, 0xada8, 0x2b60, 0x9570, 0x4978        , 0x4970, 0x64b0, #1930 0xd4a0, 0xea50, 0x6d48, 0x5ad0, 0x2b60, 0x9370, 0x92e0, 0xc968, 0xc950, 0xd4a0, #1940 0xda50, 0xb550, 0x56a0, 0xaad8, 0x25d0, 0x92d0, 0xc958, 0xa950, 0xb4a8, 0x6ca0, #1950 0xb550, 0x55a8, 0x4da 0, 0xa5b0, 0x52b8, 0x52b0, 0xa950, 0xe950, 0x6aa0, 0XAD50, #1960 0xab50, 0x4b60, 0xa570, 0xa570, 0x5260, 0xe930, 0xd950, 0x5aa8, 0x56a0, 0x96d0, #1970 0x4ae8, 0x4ad0, 0xa4d0, 0xd268, 0xd250, 0xd528, 0xb540, 0xb6a0, 0x96d0, 0x95b0, #1980 0x49b0, 0xa4b8, 0xa4b0, 0xb258, 0x6a50, 0X6D40, 0xada0, 0xab60, 0x9370, 0x4  978, #1990 0x4970, 0x64b0, 0x6a50, 0xea50, 0x6b28, 0X5AC0, 0xab60, 0x9368, 0x92e0, 0xc960, #2000 0xd4a8, 0xd4a0, 0xda50, 0x5aa8, 0x56a0, 0xaad8, 0x25d0, 0x92d0, 0xc958, 0xa950, #2010 0xb4a0, 0xb550, 0xb550, 0x55a8, 0x 4BA0, 0xa5b0, 0x52b8, 0x52b0, 0xa930, 0x74a8, #2020 0x6aa0, 0XAD50, 0x4da8, 0x4b60, 0x9570, 0xa4e0, 0xd260, 0xe93        0, 0xd530, 0x5aa0, #2030 0x6b50, 0x96d0, 0x4ae8, 0x4ad0, 0xa4d0, 0xd258, 0xd250, 0xd520, 0xdaa0, 0xb5a0, #2040 0x56d0, 0x4ad8, 0x49b0, 0xa4b8, 0xa4b0, 0xaa50, 0xb528, 0X6D20, 0xada0, 0x55b0, #2050] #数组gLanarMonth存 The month of the lunar calendar 1901 to 2050 leap months, if not, 0, per byte for two years g_lunar_month = [0x00, 0x50, 0x04, 0x00, 0x20, #1910 0x60, 0x05, 0x0  0, 0x20, 0x70, #1920      0x05, 0x00, 0x40, 0x02, 0x06, #1930 0x00, 0x50, 0x03, 0x07, 0x00, #1940 0x60, 0x04, 0x00, 0x20, 0x #1950 0x05, 0x00, 0x30, 0x80, 0x06, #1960 0x00, 0x40, 0x03, 0x07, 0x00, #1970 0x50, 0x04, 0        x08, 0x00, 0x60, #1980 0x04, 0x0a, 0x00, 0x60, 0x05, #1990 0x00, 0x30, 0x80, 0x05, 0x00, #2000   0x40, 0x02, 0x07, 0x00, 0x50, #2010 0x04, 0x09, 0x00, 0x60, 0x04, #2020 0x00, 0x20, 0x60, 0x05, 0x00, #2030 0x30, 0xb0, 0x06, 0x00, 0x50, #2040 0x02, 0x07, 0x00, 0x50, 0x03 #2050] #============= ===================================================================== from datetime import Date, datetime from C  Alendar import Calendar as Cal start_year = 1901 def is_leap_year (tm): y = tm.year return (not (y% 4)) and (y%) or (not (y%)) def-Show_month (tm): (Ly, LM, LD) = Get_ludar_date (tm) Print p Rint U "%d year%d month%D-Day "% (Tm.year, Tm.month, Tm.day), Week_str (tm), print U" \ t lunar: ", Y_lunar (ly), M_lunar (LM), D_lunar (LD) Prin        T print u "day \ t two \ t three \ four \ t five \ t six" C = Cal () ds = [D for D in C.itermonthdays (Tm.year, Tm.month)] Count = 0 for D in Ds:count + = 1 if D = = 0:print "\ T", CO                Ntinue (ly, LM, LD) = Get_ludar_date (DateTime (Tm.year, Tm.month, D)) if count% 7 = = 0:  Print d_str = str (d) if d = = Tm.day:d_str = U "*" + d_str Print D_str + d_lunar (LD) + U "\ T", Print Def this_month (): Show_month (DateTime.Now ()) def week_str ( TM): a = U ' Monday Tuesday Wednesday Thursday Friday Saturday Sunday '. Split () return A[tm.weekday ()] def d_lunar (LD): a = U '        First second day third day four Duanwu arrest When初七because holidays Penggushan 1.,234,567,892,212,22e,+28 '. Split () return a[ld-1] def m_lunar (LM):       A = U ' lunar month May June seven month August Borlnd November December '. Split () return a[lm-1] def y_lunar (ly): y = ly TG = U ' n-Xing '. Split () dz = U ' obscure not Shin hai '. Split () SX = U ' rat cow-tiger-free dragon snake horse-Goat Monkey Chicken Dog pig '. Split () return tg[(y-4)%] + dz[(y-4)%] + U ' + sx[(y-4)%] + u ' def date_diff (tm) : Return (Tm-datetime (1901, 1, 1)). Days def get_leap_month (lunar_year): flag = g_lunar_month[(lunar            _year-start_year)/2] if (lunar_year-start_year)% 2:return flag & 0x0f Else:            Return Flag >> 4 def lunar_month_days (Lunar_year, Lunar_month): if (Lunar_year < Start_year):            Return high, low = 0, ibit = 16-lunar_month; if (Lunar_month > Get_leap_month (lunar_year) and Get_leap_month (lunar_year)): ibit-= 1 if (G_lun Ar_month_day[lunar_year-start_year] & (1 <<ibit)): Low + = 1 if (Lunar_month = = Get_leap_month (lunar_year)): if (G_lunar_mont H_day[lunar_year-start_year] & (1 << (iBit-1)): High = Else:h            IgH = return (high, low) def lunar_year_days (year): Days = 0 for I in range (1, 13): (High, low) = Lunar_month_days (year, i) days + = high days + = low return days D EF Get_ludar_date (tm): Span_days = Date_diff (tm) #阳历1901年2月19日为阴历1901年正月初一 #阳历1901年1月1日到2月19日共有49 Day if (Span_days <49): Year = Start_year-1 if (span_days <19): month = 11                ;                Day = one + span_days else:month = 12;  Day = span_days-18 return (year, month, day) #下面从阴历1901年正月初一算起 span_days-=-year,       month, day = start_year, 1, 1 #计算年 TMP = Lunar_year_days (year) while span_days >= tmp:span_days = tmp year + = 1 TMP = Lunar_year_days (year) #计算月 (foo, tmp) = Lunar_month_days (year, month) while Span_days > = Tmp:span_days-= tmp if (month = = Get_leap_month (year)): (tmp, foo) = Lunar_month_ Days (year, month) if (Span_days < TMP): return (0, 0, 0) span_days-=        TMP month + = 1 (foo, tmp) = Lunar_month_days (year, month) #计算日 Day + = Span_days Return (year, month, day) #功能简单, print only This_month () for the current month

Output Result:

November 5, 2015 Thursday lunar calendar: B-year of the sheep September 24


Day 123456

221,322,543 * 4,626,246
7,928,102,912,132 beginning 113 214 third day
15 beginning 416 Beginning 517 beginning 618 beginning 719 early 820 921 decade
1.,232,243,254,265,28e,+18
8,599

Calculation Age:

From time import *    #a function to find your age    def age ():        print "Enter your Date of Birth"        d=input ("Day:" )        m=input ("Month:")        Y=input ("Year:") #get the current time in        tuple format        a=gmtime ()        # Difference in day        dd=a[2]-d        #difference in month        dm=a[1]-m        #difference on year        dy=a[0]-y        #checks If difference in day are negative        if dd<0:            dd=dd+30            dm=dm-1            #checks if difference in Month is negative when difference on day is also negative            if dm<0:                dm=dm+12                dy=dy-1        #checks if diff Erence in month was negative when difference on day was positive        if dm<0:            dm=dm+12            dy=dy-1        print "You R Current age is%s years%s Months &%s days '% (DY,DM,DD) Age            ()
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.