This topic has not yet been rated - Rate this topic

SPTimeZone.UTCToLocalTime Method

Converts the specified DateTime value from Coordinated Universal Time (UTC) to local time.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
[SecurityPermissionAttribute(SecurityAction.Assert, UnmanagedCode = true)]
public DateTime UTCToLocalTime(
	DateTime date
)

Parameters

date
Type: System.DateTime

Return Value

Type: System.DateTime
A System.DateTime structure that contains the date and time converted to their local values.

The following code example displays the date and time on which each file in a Shared Documents document library was created and last modified, converting date and time values to local time.

using (SPSite oSiteCollection = new SPSite("http://localhost"))
{
    using (SPWeb oWebsite = oSiteCollection.AllWebs["Website_Name"])
    {
        SPFileCollection collFiles = oWebsite.Folders["Shared Documents"].Files;
        SPRegionalSettings oRegionalSettings = oWebsite.RegionalSettings;
        SPTimeZone oTimeZone = oRegionalSettings.TimeZone;

        foreach (SPFile oFile in collFiles)
        {
            Console.WriteLine(oFile.Title +
            " :: " + oTimeZone.UTCToLocalTime(oFile.TimeCreated) +
            " :: " + oTimeZone.UTCToLocalTime(oFile.TimeLastModified) +
            "<BR>");
        }
    }
}
NoteNote

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ