Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8435821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:01:10+00:00 2026-06-10T07:01:10+00:00

This should be really simple, but I’m having an embarrassing amount of trouble with

  • 0

This should be really simple, but I’m having an embarrassing amount of trouble with it.

In PostgreSQL 9.1, I need to intepret a field stored as a DATE in the DB as if it were a TIMESTAMPTZ representing midnight UTC on that date. I’d like to do it in a clean and readable way that someone can come along, look at, and understand what’s happening.

The only ways I’ve found to do it so far are both very ugly. One converts it to a TIMESTAMP WITHOUT TIME ZONE then creates a TIMESTAMP WITH TIME ZONE from it by interpreting it as if it were UTC:

SELECT CAST(DATE '2012-01-01' AS TIMESTAMP WITHOUT TIME ZONE) AT TIME ZONE 'utc'

The other way is worse:

('2012-01-01'::date)::timestamptz - (current_timestamp AT TIME ZONE 'UTC' - current_timestamp)

in that it converts the date to a timestamp for midnight local time, then subtracts the time zone offset. I couldn’t find any way to get that offset as an interval natively (which seems crazy) so I landed up getting it by comparing current_timestamp in local time with current_timestamp in UTC.

The only other way I could work out used extract to get the date parts and assembled a new timestamptz from them. I won’t even show that one, it’s too ugly.

Both approaches feel all kinds of weird and wrong. Is there any sane way – standard or no – to convert in a readable and easily understood way from a DATE to a timestamptz of midnight UTC on that date?

I’m looking for something like (imaginary, won’t work)

'2012-01-01'::date AS TIMESTAMPTZ IN TIME ZONE '00:00';

or

to_timestamp('2012-01-01'::date, '00:00'::time, 'UTC');

Please point out the stupidly obvious thing I’m missing.

Note that I’m testing to make sure the date is truly right internally, not just at display, with extract(epoch from $1) where $1 is the converted date.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:01:12+00:00Added an answer on June 10, 2026 at 7:01 am

    Your first approach is correct. And it’s not that ugly, is it? In simplified Postgres syntax:

    SELECT '2012-1-1'::date::timestamp AT TIME ZONE 'UTC';
    

    Applied to a variable or column it looks even more elegant:

    SELECT mydate::timestamp AT TIME ZONE 'UTC';
    

    If you are going to enter the date manually, you can shortcut to:

    SELECT '2012-1-1 0:0'::timestamp AT TIME ZONE 'UTC'
    

    The result will always be displayed according to the local timezone of the client (i.e. with the according offset), but that has no influence on the value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this should be really simple, but im having trouble getting this to
This should be really simple but I'm having trouble with it. How do I
This should be a really simple question, but I'm still having difficulty. I have
This should be really simple, but I have tried many solutions posted on the
This seems like it should be really simple, but I'm unable to figure this
This should be really simple but I'm a javascript/jQuery rookie, so here we go:
This should be really simple but I can't get it to work, how do
This should be really simple, but what is the way to go on this.
Ok this should be really simple, but I am doing my head in here
I'm cocking this up and it should be really simple but the value of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.