簡體   English   中英

如何從當天的 00:00:00 到 11:59:59 之間從 postgresql 獲取所有數據?

[英]how can i get the all data from postgresql thats from the current day, between the hours of 00:00:00 and 11:59:59?

后端以及 postgresql 的所有東西都非常新,目前我所能做的就是

SELECT * FROM nutrition WHERE timestamp >= (extract(epoch from now())::bigint * 1000) - 86400000 AND timestamp <= (extract(epoch from now())::bigint * 1000) + 86400000

在前端使用 js,我使用 Date.now() 將時間戳存儲在數據庫中。

時間戳是我數據庫中的一列,它以 bigint 格式記錄 unix 時間,其中記錄了食物。 我想從午夜 12 點到晚上 11:59 之間獲取當天的所有數據。 謝謝。

例如,我記錄的最后一項是昨晚晚上 10 點(1663995295337 UNIX 時間),所以數據不應該包括它。

顯示時區返回;

美國/洛杉磯

解決方法如下------------------------------------------------ --------------------

 const today = new Date();
  const beginningOfDay = today.setUTCHours(7, 0, 0, 0);
  const endOfDay = today.setUTCHours(30, 59, 59, 99);

  switch (method) {
    case "GET":
      try {
        const text = `
    select 
     * 
    from 
       nutrition 
    where 
      timestamp 
    between ${beginningOfDay} and ${endOfDay}`

這是我正在尋找的解決方案,感謝您的幫助。 對不起,如果我描述性不夠。

假設按 Unix 時間,您的意思是epoch

select extract(epoch from now());
      extract      
-------------------
 1664038032.392004

 select to_timestamp(1664038032.392004);
          to_timestamp          
--------------------------------
 09/24/2022 09:47:12.392004 PDT


select 
 * 
from 
   some_table 
where 
   to_timestamp(1664038032.392004) 
between 
    current_date + '00:00:00'::time AND current_date + '23:59:59'::time

您可以將“unix 時間戳”轉換為日期,然后將其與“今天”進行比較:

where to_timestamp("timestamp")::date = current_date

這假設您名為"timestamp"的列並不是真正的timestamp

如果該列實際上沒有存儲秒數(這將是一個 unix 紀元),但是毫秒你需要to_timestamp("timestamp"/1000)::date代替(如果你使用了適當的timestamptz或至少是timestamp數據類型)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM