Adjusting Default Timezone in code

Is there anyway to adjust default timezone in code?

I have a global site with sub lists for each region and they are all on different time

May 20th, 2015 10:42pm

Hi

yes provided you have different site have different regional setting.

use this method LocalTimeToUTC()

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>");
        }
    }
}

https://dhendry.wordpress.com/2012/06/30/converting-times-utc-to-local-sharepoint/

https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.sptimezone.utctolocaltime%28v=office.12%29.aspx?f=255&MSPPError=-2147217396

Free Windows Admin Tool Kit Click here and download it now
May 21st, 2015 1:21am

where would I place this in my code?
May 21st, 2015 6:09am

You can make a simple console application,place this code and run..it should work 

Free Windows Admin Tool Kit Click here and download it now
May 21st, 2015 6:12am

I am not overly familiar with how to do this is there a sample you can show?

I am assuming this is the code but not sure how to plug into my .aspx file.

[SecurityPermissionAttribute(SecurityAction.Assert, UnmanagedCode = true)]
public DateTime UTCToLocalTime(
DateTime date
)

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>");
        }
    }
}

May 21st, 2015 6:15am

Hi

In Aspx file what activity you are performing put the logic where you are binding the control with date time field.

Free Windows Admin Tool Kit Click here and download it now
May 21st, 2015 6:22am

is there not a way to have it impact  the entire form?
May 21st, 2015 6:25am

This is the specific piece of code I would like it to impact

<SharePoint:FormField runat="server" id="ff63{$Pos}" controlmode="New" fieldname="TRIAGE_Date" __designer:bind="{ddwrt:DataBind('i',concat('ff63',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@TRIAGE_Date')}" />

Free Windows Admin Tool Kit Click here and download it now
May 21st, 2015 6:26am

Hi Pinkston,

Please refer this link : 

http://solutionz4u.blogspot.com/2011/05/creating-simple-console-application-in.html

replace your code in Main()

hope this helps :) 

May 21st, 2015 6:27am

you can also refer this : 

http://www.c-sharpcorner.com/UploadFile/0f100d/creating-a-simple-console-application-in-sharepoint-2010/

Free Windows Admin Tool Kit Click here and download it now
May 21st, 2015 6:30am

Sorry this one completely lost me
May 21st, 2015 6:38am

sorry again lost, I am just trying to add some code to my existing .aspx that will change the datetime to match local datetime of the users machine not the server from which the sp app exists.
Free Windows Admin Tool Kit Click here and download it now
May 21st, 2015 6:41am

is there a way to use this

SPTimeZone timeZone = SPContent.Current.Web.RegionalSettings.TimeZone;
listItem
[fieldName] = timeZone.LocalTimeToUTC(localTime);

May 21st, 2015 7:12am

CultureInfo ci = new CultureInfo("en-GB");

SPRegionalSettings rs = new SPRegionalSettings(web, false);

rs.LocaleId = (uint)ci.LCID;

rs.Time24 = rs.GetDefaultTime24((uint)ci.LCID);

rs.CalendarType = (short)rs.GetDefaultCalendarType(ci.LCID);

web.RegionalSettings = rs;

web.Update();

May 22nd, 2015 2:50am

Ashish, where do I plug this?
May 25th, 2015 4:24pm

Help Ashish!!!!!!!!!!!
Free Windows Admin Tool Kit Click here and download it now
May 28th, 2015 1:23am

Hi,

If this is a one time activity create a console application and do it. 

If not you can write a feature. and write the code.

Code for Site feature can we like this. Also you can write code on the feature deactivation to set some other setting.

SPSite site = properties.parent as SPSite;

SPWeb web = site.RootWeb;

CultureInfo ci = new CultureInfo("en-GB");

SPRegionalSettings rs = new SPRegionalSettings(web, false);

rs.LocaleId = (uint)ci.LCID;

rs.Time24 = rs.GetDefaultTime24((uint)ci.LCID);

rs.CalendarType = (short)rs.GetDefaultCalendarType(ci.LCID);

web.RegionalSettings = rs;

web.Update();

Hope this helps. Sorry for the delay!

May 30th, 2015 11:37am

Ashish,

Does this go right in my ASPX code?

Free Windows Admin Tool Kit Click here and download it now
May 31st, 2015 7:08pm

1. You have to open visual studio. > Create Feature (Site Scope) > Write the Code (deploy to site collection)

2. Goto Site Settings > Features > Activate the feature.

--Other Approach-----------------------------------------------------------------------------------

1. Open Visual Studio

2. Create console application > add reference to Dlls (Microsoft.Sharepoint - please note this will run on the  sharepoint server - otherwise csom comes into picture)

3. Code

using(SPSite osite = new SPSite("url"))
{
  
  //rest code from Above.

}

>>But wait :) you can do all this from UI as well. If just one time activity on an already deployed site.

Site Setting (on right pannel) > Under Site Administration > Regional Settings 

June 1st, 2015 2:30am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics