Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • JacoboPolavieja 62 posts 84 karma points
    Aug 19, 2010 @ 20:26
    JacoboPolavieja
    0

    Add hours to retrieved time

    Hello all,

     

    As it seems there's no way to manage a different Timezone than the server's I'm requested to add 6hours when displaying node's creation time... i've had a loook at the forum but found nothing so far.

    Is there an easy way to add some time to a retrieved date/hour?

     

    Thanks a lot!

  • Rich Green 2246 posts 4008 karma points
    Aug 19, 2010 @ 21:32
    Rich Green
    0

    Hi Jacobo,

    You can use DateAdd, all the information is here http://our.umbraco.org/wiki/reference/umbracolibrary/dateadd

    Post back if you need further info.

    Rich

  • Rich Green 2246 posts 4008 karma points
    Aug 19, 2010 @ 21:34
    Rich Green
    1

    Here you go...

     

    xsl:value-of select="umbraco.library:DateAdd($currentPage/@createDate, 'h', 6)"/>
  • Paul Blair 466 posts 731 karma points
    Aug 19, 2010 @ 22:32
    Paul Blair
    1

    The problem with just adding a set amount of time is that it won't always be correct if you need to adjust for Daylight Savings Time.

     I've added a routine to some of my projects that uses the .NET TimeZoneInfo class:

        public static string getLocalTime(int nodeID)
        {
            string nzTimeZoneKey = "New Zealand Standard Time";
            TimeZoneInfo nzTimeZone = TimeZoneInfo.FindSystemTimeZoneById(nzTimeZoneKey);
            string usTimeZoneKey = "US Mountain Standard Time";
            TimeZoneInfo usTimeZone = TimeZoneInfo.FindSystemTimeZoneById(usTimeZoneKey);

            Node SnowReport = new Node(nodeID);
            DateTime nzDateTime = TimeZoneInfo.ConvertTimeFromUtc(SnowReport.UpdateDate.ToUniversalTime(), nzTimeZone);

            return nzDateTime.ToString("d MMM yyyy") + " " + nzDateTime.ToString("HH:mm");
        }

    Convert the server time to UTC and then convert the UTC to local time.

  • JacoboPolavieja 62 posts 84 karma points
    Aug 20, 2010 @ 10:44
    JacoboPolavieja
    0

    Thanks a lot Rich Green, that worked perfectly!

     

    Paul, I'm using just XSLT at the moment but your solution seems a pretty elegant and good one... When I have the time to really do things properly I'm sure I'll revisit this thread to get your code ;).

     

    Thanks a lot to both!

  • Paul Blair 466 posts 731 karma points
    Aug 20, 2010 @ 12:34
    Paul Blair
    0

    I have also used a similar function in other projects and called them from within XSLT - it works very well. I might go make a suggestion in the Umbraco 5 forum :)

Please Sign in or register to post replies

Write your reply to:

Draft