Convert date from UTC to PST

  • Hi Guys,

     

    Is there any way to convert date format from UTC to PST ???

    It's urgent...

    Thanks and Regards

    Ashu Garg

  • I believe the time difference is -8 hours so try this...

    DATEADD(hh,-8,yourUTCdatetime)

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thanks Jeff,

    But the problem is that PST is adjusted due to "Day Light Saving" system and hence the difference between GMT and PST will vary by 1 hour.

    Please tell me how to handle this problem in this case.

    Thanks

    Ashu

  • This was written for Eastern Time Zone and does take into account the diff between ST and DST

     

    CREATE PROCEDURE Convrt_GMT_To_Eastern

    @DateIn datetime,

    @DateOut datetime OUTPUT

    AS

    --This Code Converts GMT to Local Time

    Declare @DateUse datetime

    Declare @TimeChg VARCHAR(20)

    Declare @DayVal NUMERIC

    Declare @GMT datetime

    DECLARE @DiffDST int,

     @DiffST int

    SET @DiffDST = -4

    SET @DiffST = -5

    SET @DateUse = @DateIn --Get Current DateTime

    If Month(@DateUse) >= 10 --Check To See if Date is Oct, Nov, or Dec

     BEGIN

      Set @TimeChg = '10/31/' + Cast(Year(@DateUse) as varchar) --Use Oct 31 as strt Point

     END

    ELSE

      Set @TimeChg = '4/1/' + Cast(Year(@DateUse) as varchar) --Use April 1 as start point

    Set @DayVal = DatePart(weekday,@TimeChg) --Is the Start Point a Sunday

    If @DayVal <> 1 --If Not then when is the proper Time Change Sunday

     If DatePart(m,@TimeChg) = 4 --Is the base the April Value

      Begin

       Set @TimeChg = DateAdd(d,8 - @DayVal,@TimeChg) --For April it is the first Sunday of the Month

       Set @TimeChg = DateAdd(hh,7, @TimeChg)

      End

     Else

      BEGIN

       Set @TimeChg = DateAdd(d,-@DayVal + 1,@TimeChg) --For Oct it is the last Sunday of the Month

       Set @TimeChg = DateAdd(hh,6, @TimeChg)

      END

    If DatePart(m,@TimeChg) = 4 --If Time Change is Based on April

     Begin

      If @DateUse < @TimeChg --If DateUse < The Time Change Date & Time

       Begin

        Set @DateUse = DateAdd(hh,@DiffST,@DateUse) --GMT 5 hours ahead for EASTERN Daylight Savings

       End

      Else

       Set @DateUse = DateAdd(hh,@DiffDST,@DateUse) --GMT 4 hours ahead for EASTERN Standard

     End

    Else

     If @DateUse < @TimeChg --If DateUse < The Time Change Date & Time

      Begin

       Set @DateUse = DateAdd(hh,@DiffDST,@DateUse) --GMT 4 hours ahead for EASTERN Standard

      End

     Else

      Set @DateUse = DateAdd(hh,@DiffST,@DateUse) --GMT 5 hours ahead for EASTERN Daylight Savings

      

    Set @DateOut = @DateUse  --Date time to eastern TZ

    GO

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply