time difference calculating postgres

  • Displays the current time

    select now()
                  now
    -------------------------------
     2019-10-19 01:03:13.525727+08
    (1 行记录)
  • Calculate the time difference

    • The date_part () function. Separate part can return date

      select date_part('day',now())
      
       date_part
      -----------
              19
      (1 行记录)
      • Role: Calculation of poor, poor month, day difference, the difference between hours, minutes and poor

        select date_part('day',now()-create_time) from t_test (实际应用,看具体输出)
        
        select date_part('day',now()::timestamp-'2018-01-10 10:12:15::timestamp')
         date_part
        -----------
               646
        (1 行记录)
    • extract () function to calculate time difference

      select extract(day from now()-create_time) from t_test;
    • Add or subtract

      select now();
                    now
      -------------------------------
       2019-10-19 01:11:24.264592+08
      (1 行记录)
      select now()-interval '2 day';
                 ?column?
      -------------------------------
       2019-10-17 01:11:24.264592+08
      (1 行记录)
      select now()+interval '2 month';
                 ?column?
      -------------------------------
       2019-12-19 01:11:24.264592+08
      (1 行记录)
    • Calculating a first day of the month / week

    select date_trunc('month',now());
           date_trunc
    ------------------------
     2019-10-01 00:00:00+08
    (1 行记录)
    
    select date_trunc('week',now());
           date_trunc
    ------------------------
     2019-10-14 00:00:00+08
    (1 行记录)
  • time unit

    • Year: year
    • Month: month (0-11)
    • S: second
    • Min: minutes (0-59)
  • Reference documents:

    time difference calculating postgresql

Guess you like

Origin www.cnblogs.com/MyUniverse/p/11701627.html