How to Convert UTC to localTIme(C# )

I have got the value of user account's lastlogon time. Its type is Int64.
This value is stored as a large integer that represents the number of 100 
nanosecond intervals since January 1, 1601 (UTC)(Refer to MSDN).
I don't know how to convert this value to localTime.

The following is my code.
################################################################
            DirectoryEntry deUser = new DirectoryEntry(ldappath);
            DirectorySearcher src = new DirectorySearcher(deUser);
            src.Filter = "(&(objectClass=user)(SAMAccountName=" + 
accountName + "))";
            SearchResult result = src.FindOne();
            ResultPropertyCollection accountProperties = result.Properties;

            foreach (object lastlogon in accountProperties["lastlogon"])
{
 //Int64 lastlogon =129046401906875000 ;//this is the value of user 
account's lastlogon time.
 double aa=Convert.ToDouble(lastlogon.ToString()); // I am not sure that 
this is a correct and good method
 DateTime dtZone = new DateTime(1601, 1, 1, 0, 0, 0);
 dtZone.AddSeconds(aa/10000000);//10^7 ,AddSeconds method needs a double 
type parameter.
 lbLastLogon.Text = dtZone.ToLocalTime().ToString(); //it displays 1601-1-1 
8:0;0,it is
                                                        not correct 
localtime
}
##################################################################################




I try to use number dirctly as following. Then It returns "2009-12-7 
6:16:30" ,Year ,month,day are right ,but the rest is not right.I am in 
China.It may need add 8 hours additionally.
**********************************************************
DateTime dtZone = new DateTime(1601, 1, 1, 0, 0, 0);
lbLastLogon .Text =dtZone 
..AddSeconds(12904640190687500000/1000000000).ToString ()
**********************************************************


0
eagle
12/7/2009 10:03:37 AM
dotnet.languages.csharp 1931 articles. 0 followers. Follow

5 Replies
2247 Views

Similar Articles

[PageSpeed] 39

������ wrote:
> I have got the value of user account's lastlogon time. Its type is Int64.
> This value is stored as a large integer that represents the number of 100 
> nanosecond intervals since January 1, 1601 (UTC)(Refer to MSDN).
> I don't know how to convert this value to localTime.
> 
> The following is my code.
> ################################################################
>             DirectoryEntry deUser = new DirectoryEntry(ldappath);
>             DirectorySearcher src = new DirectorySearcher(deUser);
>             src.Filter = "(&(objectClass=user)(SAMAccountName=" + 
> accountName + "))";
>             SearchResult result = src.FindOne();
>             ResultPropertyCollection accountProperties = result.Properties;
> 
>             foreach (object lastlogon in accountProperties["lastlogon"])
> {
>  //Int64 lastlogon =129046401906875000 ;//this is the value of user 
> account's lastlogon time.
>  double aa=Convert.ToDouble(lastlogon.ToString()); // I am not sure that 
> this is a correct and good method
>  DateTime dtZone = new DateTime(1601, 1, 1, 0, 0, 0);
>  dtZone.AddSeconds(aa/10000000);//10^7 ,AddSeconds method needs a double 
> type parameter.
>  lbLastLogon.Text = dtZone.ToLocalTime().ToString(); //it displays 1601-1-1 
> 8:0;0,it is
>                                                         not correct 
> localtime
> }
> ##################################################################################
> 
> 
> 
> 
> I try to use number dirctly as following. Then It returns "2009-12-7 
> 6:16:30" ,Year ,month,day are right ,but the rest is not right.I am in 
> China.It may need add 8 hours additionally.
> **********************************************************
> DateTime dtZone = new DateTime(1601, 1, 1, 0, 0, 0);
> lbLastLogon .Text =dtZone 
> .AddSeconds(12904640190687500000/1000000000).ToString ()
> **********************************************************
> 

The DateTime type uses a 100 nanosecond value, but with the origin at 
0001-01-01 instead of 1601-01-01, so you just have to add the 
difference. Use DateTimeKind.Utc to create an UTC DateTime value:

DateTime utc = new DateTime(lastlogon + 504911232000000000, 
DateTimeKind.Utc);

Now you can just convert it to local time:

DateTime local = utc.ToLocalTime();


-- 
G�ran Andersson
_____
http://www.guffa.com
0
ISO
12/7/2009 2:53:58 PM
1.What does it mean by 504911232000000000?

2.You said"with the origin at 0001-01-01 instead of 1601-01-01", but why 
MSDN http://msdn.microsoft.com/en-us/library/ms676823(VS.85).aspx 
says"intervals since January 1, 1601 (UTC)."?




0
eagle
12/8/2009 1:30:14 AM
"������" <eagle@no.spam.net> wrote:

>1.What does it mean by 504911232000000000?

He told you that.  It's the number of 100ns ticks between 0001-01-01 and
1601-01-01.

>2.You said"with the origin at 0001-01-01 instead of 1601-01-01", but why 
>MSDN http://msdn.microsoft.com/en-us/library/ms676823(VS.85).aspx 
>says"intervals since January 1, 1601 (UTC)."?

The Last-Login attribute starts at 1601-01-01.  The number in the DateTime
type is origined at 0001-01-01.  The number you showed (129046401906875000)
is about 408 years, so it is must be based from 1601.  So, if you want to
initialize a DateTime variable with that integer, you need to add the
difference between 0001 and 1601.

The ToLocalTime method of the DateTime class will take your timezone into
account.

class What
{
    public static void Main()
    {
        Int64 i1 = 129046401906875000;
        Int64 i2 = 504911232000000000;
        DateTime dt1 = new DateTime( i1 + i2 );
        DateTime dt2 = new DateTime( 1601, 1, 1, 0, 0, 0 );
        Console.WriteLine( dt1 );
        Console.WriteLine( dt1.ToLocalTime() );
        Console.WriteLine( dt2 );
    }
}
-- 
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
0
Tim
12/8/2009 6:01:50 AM
������ wrote:
> 1.What does it mean by 504911232000000000?

It's the number of ticks between 0001-01-01 and 1601-01-01, i.e. the 
value that you get from new DateTime(1601,1,1).Ticks.

> 2.You said"with the origin at 0001-01-01 instead of 1601-01-01", but why 
> MSDN http://msdn.microsoft.com/en-us/library/ms676823(VS.85).aspx 
> says"intervals since January 1, 1601 (UTC)."?

Because that is a FILETIME value, not a DateTime value.

-- 
G�ran Andersson
_____
http://www.guffa.com
0
ISO
12/8/2009 2:07:59 PM
Thank you very much! 


0
eagle
12/8/2009 2:12:00 PM
Reply:

Similar Artilces:

How to Convert UTC to localTIme(C# )
I have got the value of user account's lastlogon time. Its type is Int64. This value is stored as a large integer that represents the number of 100 nanosecond intervals since January 1, 1601 (UTC)(Refer to MSDN). I don't know how to convert this value to localTime. The following is my code. ################################################################ DirectoryEntry deUser = new DirectoryEntry(ldappath); DirectorySearcher src = new DirectorySearcher(deUser); src.Filter = "(&(objectClass=user)(SAMAccountName=" + accountNa...

Convert text to time value
I have a series of time values in a 'General' format. They are of the type: 184525 Which is 18:45:25 or 6:45:25 pm. A time which is am would be of the type: 12345 Which is 1:23:45 am. Is there a way to convert those 'General' values to an Excel serial so that I can figure out the difference between two times? I've seen a bunch of examples on the net, but none of those that I have found deal with this format that I can tell. Thanks. Hi try =--TEXT(A1,"00:00:00") -- Regards Frank Kabel Frankfurt, Germany "Andy" <amelton@gmail.com> schrieb...

how to convert date
Hi, I'm looking for some method to convert mail date, in format: eg. "Sun, 18 Sep 2005 20:57:08 +0200", to computer local time. I tried CTime but without resoults. m. Have you tried COleDateTime::ParseDateTime()? m.wski21.usunto@aust.com wrote: > Hi, > > I'm looking for some method to convert mail date, in format: > eg. "Sun, 18 Sep 2005 20:57:08 +0200", to computer local time. > I tried CTime but without resoults. > > m. >I'm looking for some method to convert mail date, in format: >eg. "Sun, 18 Sep 2005 20:57:08 +0200&qu...

Excel: Auto converting text to numbers
I am downloading an Excel sheet, and the numbers come in as text. It basically comes in as "33 %" but Excel registers this as text, not a percentage. I have a cell that will be used to add the numbers, but since they are text it doesn't work. Given this information, is there a way to convert the imported data into numbers. I would prefer to include this into my formula. The potential numbers are: 0, 1, 2, 3, 4, 0 %, 25 %, 33 %, 50 %, 67 %, 75 %, 100 %, and N/A I would prefer a function, again if possible, that could convert any number. Please note, the space between the nu...

How do I convert dates to text keeping the format?
I'm trying to convert a column of data in date format *m/d/yyyy to a text format without converting to serial numbers. Ie: I want to retain the mm/dd/yyyy format. Is there a way to do this? =TEXT(A1,"MM/DD"/YYYY") "sprlarry" <sprlarry@discussions.microsoft.com> wrote in message news:69669AA6-FD15-47D7-843D-FC768728BF7A@microsoft.com... > I'm trying to convert a column of data in date format *m/d/yyyy to a text > format without converting to serial numbers. Ie: I want to retain the > mm/dd/yyyy format. Is there a way to do this? That ...

how to convert excel's .cvf file to .csv file
...

Convert Access97 to 2000
Hello, we're currently running access97 and would like to convert it to 2000, but we don't know what is the administrator password for this database. Also this database is running on multi user and have difference permission for diffence users. Could someone help me how to do make this happen but keep the currently permission retaint. Thanks ...

Savings a/c cashflow doesn't show withdrawals
Money 2003 When I do a cashflow forecast for my savings a/c it doesn't take into account transfers to my "cash" account - the one I use to keep track of what I spend my cash on. Consequently when I do the cashflow it gives an unreal projection of savings. I wish I could save $3000 in 2 months but it's not going to happen... ...

Convert 2000 Calendar to web page
Greetings, When I convert my calendar for 2005 to a webpage, the page is off by 1 day. Is there a template or fix available to fix this? Thanks, Duane I can edit the html file but this should not be the case. Fixes? Suggestions...other than use Apple? "Duane Perry" <dlp_sr@yahoo.com> wrote in message news:yZRtd.5561$0r.1710@newsread1.news.pas.earthlink.net... > Greetings, > > When I convert my calendar for 2005 to a webpage, the page is off by 1 day. > Is there a template or fix available to fix this? > > Thanks, > > Duane > > Duane, ...

XML Note convert to DataSet
Hello, I have this function: object acmResponse = acmLogin.acmString("4001", "", paramFormLogin + paramUserBasics);System.Xml.XmlNode[] acmNodes = (System.Xml.XmlNode[])acmResponse; What I have todo, to convert the XML Object in the DataSet Object? Thank you Matthias ...

Problem converting from Quicken to M2005
My Quicken files are mostly investment related, and generally converted fine. However all bonds (regular and muni's) converted as Investment type: Mutual Fund, not Bond. (1) How do I prevent that, (2) How do you change the Investment Type for an item? Thank you. In microsoft.public.money, Mike wrote: >My Quicken files are mostly investment related, and generally >converted fine. However all bonds (regular and muni's) converted as >Investment type: Mutual Fund, not Bond. (1) How do I prevent that, (2) Money typically converts custom data types from Quicken into funds. I thou...

Convert
Is it possible to convert a Money file created in the USA version to that of the UK version? Thanks in advance The general way is QIF Export then Import. It's involved and has limitations like loan accounts don't QIF. See http://www.bollar.org/msmoney/#Q1. "Crispy" <nowayspammers@hotmail.com> wrote in message news:uQKSfzfyDHA.2500@TK2MSFTNGP09.phx.gbl... > Is it possible to convert a Money file created in the USA version to that of > the UK version? ...

Numbers converting to decimal
I a trying to figure out why when I type 11 and automatically converts it to .11, if I type 11. it will stay 11,if I change all the cells to text then back to number they willstay. I have checked the formatting of the cells, it even happens when I open a brand new worksheet. Any ideas? Thanks Dawn Hi Dawn, Tools>Option>Edit, uncheck Fixed Decimal -- Kind Regards, Niek Otten Microsoft MVP - Excel "DawnP" <anonymous@discussions.microsoft.com> wrote in message news:c3cf01c48a05$d75359d0$a501280a@phx.gbl... > I a trying to figure out why when I type 11 and &...

Converting Quicken 2004 to Money
Quicken 2004 has many bugs, and I have had it. The most recent being that it doesn't work AT ALL now that it is the year 2004. I have had to change the date on my computer today to open it. I want to get Money instead, however I do not know if Money can get my data from the 2004 version. Does anybody know for sure? Yes is the answer to the question you posed. No is the answer to the question you are getting to but didn't pose. M04 imports Q03 and earlier. If the past predicts the future M05 will import Q04. "Colin" <anonymous@discussions.microsoft.com> wrote ...

Converting from AOL to Outlook
Is there an efficient way for me to convert my AOL address book over to Outlook? Before I buy Outlook, I want to be sure I cam make the conversion without loosing the large address book I have on AOL. "Herb Clader" <Herb Clader@discussions.microsoft.com> wrote in message news:44A9739C-A94F-49AB-BF08-31D78A86CCB7@microsoft.com... > Is there an efficient way for me to convert my AOL address book over to > Outlook? > > Before I buy Outlook, I want to be sure I cam make the conversion without > loosing the large address book I have on AOL. AOL's help p...

Customizing "Convert Campaign Response"
We have created custom Fields in the Campaign Response entity that we would like to populate on the Lead Entity when we do a conversion. However it doesn't seem like this is possible from the Data Map. Is there a way to modify what fields are carried forward during a conversion, or do I need to find a different method? Thank you in advance. Hi, As I had some time left, I tried to figure this one out. To my surprise I can't find ANY direct link between a converted campaign response and a new lead. There is only an indirect link, through the campaign entity. But that does not ...

Problem with AFX_THREAD_STATE class converting from VS6.0 to VS.NET 2003
I have inherited some code that works in VS6.0. It has code snippets like the following: _AFX_THREAD_STATE* pThreadState = AfxGetThreadState(); pThreadState->m_nLastHit = 0; and _AFX_THREAD_STATE* pThreadState = AfxGetThreadState(); CToolTipCtrl* pToolTip = pThreadState->m_pToolTip; It then does things with these values. In VS.NET, there is no member variable in _AFX_THREAD_STATE that corresponds to m_nLastHit or m_pToolTip. Have these been replaced by something similar in VS.NET 2003 that I can use? #if (_MFC_VER < 0x0700) _AFX_THREAD_STATE* pThreadState = AfxGetThrea...

Convert English language document to Hindi
Is there any software available to convert an English language text document to Hindi language document by using dictionary based translation (not transliteration). For example: The sentence : My name is Joseph Should be converted to : ???? ??? ????? ?? ...

Converting Hours an minutes just into minutes #2
Is thier a formula to use to change hous and minutes, just into minutes ex: 01:30 (1 hour and 30 minutes) into 90 minutes. And how do you format the cells. Thank you Select the cell and change the format to: [m] -- Gary's Student "Six Sigma Blackbelt" wrote: > Is thier a formula to use to change hous and minutes, just into minutes ex: > 01:30 (1 hour and 30 minutes) into 90 minutes. And how do you format the > cells. > > Thank you ...

How do I stop (C) becoming @ ?
The subject title says it all. If you click on Tools | Autocorrect you will see that listed near the top. Just click on the entry and then click on the Delete button to get rid of it. Hope this helps. Pete On Jul 31, 2:13=A0pm, Alan Secker <a...@asandco.co.uk> wrote: > The subject title says it all. Type (c) but before pressing enter, move the cursor away from the end of the cell so it's NOT after the closing paren. When you enter, it won't change. HTH Bob Umlas Excel MVP "Pete_UK" <pashurst@auditel.net> wrote in message news:dee8f91c-d100-4d6a-a548-f...

converting chart to spreadsheet?
how can i revert back to my spreadsheet from a chart? thanks You mean you have a chart but don't have the data it was created from? This article in the Microsoft knowledge base describes a macro that will extract data from a chart. http://support.microsoft.com/default.aspx?scid=kb;en-us;137016 - Jon ------- Jon Peltier, Microsoft Excel MVP Tutorials and Custom Solutions http://PeltierTech.com _______ "excel chart" <excel chart@discussions.microsoft.com> wrote in message news:7E415558-89B6-457B-A340-2A8CE2BA5521@microsoft.com... > how can i revert back to my s...

graphics converter not working
I am using Publisher 2000 to create a newsletter. It has been going well but now that I'm ready to insert pictures from files, it won't work. I get the message saying that Publisher can't convert this picture (it's either that it doesn't recognize the format or there was an error installing the graphics converter and to go to to Start > Control Panel > Add/Remove Programs > Highlight the Publisher program and choose "Change". When prompted, choose "Add Features". Then be sure to select to run ALL from your computer so that everything is...

Convert VC6.0 CString::Init() code to VC7.1.
How to convert the following code to VC7.1? // Need to declare a descendant of CString to allow access to the Init() member class CStackString : public CString { public: CStackString & operator=(CString const &s); CStackString & operator=(CStackString const &s); using CString::Init; // Make public }; inline CStackString & CStackString::operator=(CString const &s) { CString::operator=(s); return *this; } inline CStackString & CStackString::operator=(CStackString const &s) { CString::operator=(s); return *this; } class CStackValue...

Converting Lotus 123 to Excel
Hi there Is there a staightforward way of converting Lotus 123 worksheets to Excel 2000 without using any third Party Apps and without losing any of the Formula functionality from 123 ? "Paul Duane" <paul.duane@leaseplan.ie> wrote... >Is there a staightforward way of converting Lotus 123 >worksheets to Excel 2000 without using any third Party >Apps and without losing any of the Formula functionality >from 123 ? ..123 files? There are only 3 pieces of software that do anything with them: Lotus 123 itself (32-bit versions), Quattro Pro 10, and DataViz's Conv...

converters
a few years ago I did all my proposals in Apple. Now I want to retrieve some of the data using my Dell and Windows tells me the Apple 31/2 floppy is not formatted! anyone know of a conveter so I can open these floppies? thanks Can u not open these up with a (borrowed, hired or purchased) Apple then eMail / upload those files over the Internet to whatever you need? Hany "WOT231" <jkcorrie@mts.net> wrote in message news:148001c37f19$39d44b30$a001280a@phx.gbl... > a few years ago I did all my proposals in Apple. Now I > want to retrieve some of the data using my Del...