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)
Syntax
'Declaration
<SecurityPermissionAttribute(SecurityAction.Assert, UnmanagedCode := True)> _
Public Function UTCToLocalTime ( _
date As DateTime _
) As DateTime
'Usage
Dim instance As SPTimeZone
Dim date As DateTime
Dim returnValue As DateTime
returnValue = instance.UTCToLocalTime(date)
[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.
Examples
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.
Dim siteCollection As New SPSite("https://localhost")
Dim site As SPWeb = siteCollection.AllWebs("Site_Name")
Dim files As SPFileCollection = site.Folders("Shared Documents").Files
Dim regionalSettings As SPRegionalSettings = site.RegionalSettings
Dim timeZone As SPTimeZone = regionalSettings.TimeZone
Dim file As SPFile
For Each file In files
Console.WriteLine(SPEncode.HtmlEncode(file.Title) _
& " :: " & timeZone.UTCToLocalTime(file.TimeCreated) & " :: " _
& timeZone.UTCToLocalTime(file.TimeLastModified) & "<BR>")
Next file
using (SPSite oSiteCollection = new SPSite("https://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>");
}
}
}
Note
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.