다음을 통해 공유


SPTimeZone.LocalTimeToUTC Method

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

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public Function LocalTimeToUTC ( _
    date As DateTime _
) As DateTime
'Usage
Dim instance As SPTimeZone
Dim date As DateTime
Dim returnValue As DateTime

returnValue = instance.LocalTimeToUTC(date)
public DateTime LocalTimeToUTC(
    DateTime date
)

Parameters

  • date
    Type: System.DateTime

    A System.DateTime object that represents the local date and time value to convert.

Return Value

Type: System.DateTime
A System.DateTime object that contains the date converted to UTC.

Examples

The following code example converts a date and time value to UTC and displays the converted value.

Dim siteCollection As New SPSite("https://localhost")
Dim site As SPWeb = siteCollection.AllWebs("Site_Name")

Dim regSettings As SPRegionalSettings = site.RegionalSettings
Dim timeZone As SPTimeZone = regSettings.TimeZone

Dim dateTime As New System.DateTime(2004, 7, 15, 9, 5, 30, 0)

Console.WriteLine(timeZone.LocalTimeToUTC(dateTime).ToString())
using (SPSite oSiteCollection = new SPSite("https://localhost"))
{
    using (SPWeb oWebsite = oSiteCollection.AllWebs["Website_Name"])
    {
        SPRegionalSettings oRegionalSettings = oWebsite.RegionalSettings;
        SPTimeZone oTimeZone = oRegionalSettings.TimeZone;
        System.DateTime dtDateNew = new System.DateTime(2007, 7, 15, 9, 5, 30, 0);
        Console.WriteLine(oTimeZone.LocalTimeToUTC(dtDateNew).ToString());
    }
}

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.

See Also

Reference

SPTimeZone Class

SPTimeZone Members

Microsoft.SharePoint Namespace