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
4014 Views

Similar Articles

[PageSpeed] 20

������ 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:

C: drive copy / backup to D:
I have installed a second hard drive (D:) which I intend to be a backup for the C: drive. All I would like to do is copy the entire C: drive to the D: drive on a weekly schedule. I've looked into the Backup utility in XP and it's close to what I want, but it makes a bkf file rather than a copy. This is not as convenient for me (I'd like to be able to manually examine the files on D:) also I read the bkf file is corrupted even with a single bit error. I've looked at some other freeware programs but its the same deal, although the compressed format differs from product ...

how to convert to metric system in visio 2003?
how to convert to metric system in visio 2003? On Tue, 3 Mar 2009 00:00:00 -0800, alsari <alsari@discussions.microsoft.com> wrote: >how to convert to metric system in visio 2003? Don't be reluctant to tell us what you want to do. We can only guess otherwise. Do you want to use metric rather that imperial measurement templates and stencils? Do you want to convert current diagrams to metric? Do you just want to show measurements in metric units? Metric templates and stencils may not have been installed on your system. Use menu Tools -> Options to set the default measurement t...

Can I convert a column into a row?
Excel Question. I simply want to convert a column of figures [each in different cells] into a row with a similar number of cells. Must be simple, but can't seem to find how to do it. Thanks for any replies, guys!! Select the cells in the column you want to paste in the row. copy (control C). Select the same number of cells in a row, click Edit > Paste Special. Select transpose. Click ok. - Mangesh "Lenworth" <Lenworth@discussions.microsoft.com> wrote in message news:CBEBCC94-3B52-46D1-934C-224E3C234E6B@microsoft.com... > Excel Question. I simply want to convert ...

Convert 'Today' as double
C#. How do I convert today's date as a double type value? I tried: double weekOf; weekOf = ToDouble(DateTime.Today); Response.Write(weekOf.ToString()); It displayed: 2.xxxxE-266 That is a very, very small frational number. Alan T wrote: > C#. > > How do I convert today's date as a double type value? > > I tried: > > double weekOf; > > weekOf = ToDouble(DateTime.Today); This can't be what you tried because it wouldn't have compiled. And if you had tried weekOf = Convert.ToDouble(DateTime.Today) ...

convert julian date to gregorian date
what is the easiest way to convert a Julian date to a Gregorian date in Excel? Hi see: http://www.cpearson.com/excel/jdates.htm -- Regards Frank Kabel Frankfurt, Germany ammaravi wrote: > what is the easiest way to convert a Julian date to a Gregorian date > in Excel? ...

Access 97 DB convert to access 2002
Hello I would like to convert to the new 2002 access from 97. I need to be able to keep all the GUI interfaces that will make the change look seemles. I posted earlier and received an executable, but the email service did not allow it to come through. Can you resend for give directions? Thanks. ...

Converting Text to Date
Hi, Is anybody can tell me, how to convert from text data to Date?. I have a table imported from Text file, text file only recognize text & number only. One of the field (we call: TxtDate) they have is '20070515" (yyyymmdd) has to be in date format. How can I convert it to our regular format (mm/dd/yy), so I can calculate it? I tried using Format(mid(TXTDate,5,2)&"/"mid(7,2)&"/"mid(3,2),"mm/dd/yy"). It works, but I still can't use it as a date, so I can't put a parameter on it. Thanks in advance KF -- Message posted via AccessMo...

How to convert BSTR* to Cstirng by using MFC
Hi, I get a BSTR* from activex control: BSTR* STR; m_AXCTL.GetMyStr(STR); and, the prototype of GetMyStr is void* GetMyStr(BSTR FAR * strResult) { ...... } how do I Convert STR to a CString to do this: AfxMessageBox(strConvertFromBSTR); thanks. ...

I need a formula that will multiply #'s in column C by 25.37869%
I have a spread sheet of 47 rows. I need to multiply each row in column C by 25.378692%. I need the totals to appear in column D and the remaining balance to appear in column E. In D1: =C1*25.378692% In E1: =C1-D1 Copy down to fill 47 rows -- Kind regards, Niek Otten Microsoft MVP - Excel "Maggie" <Maggie@discussions.microsoft.com> wrote in message news:7F25BFE2-F1D1-4CE5-85D3-0038AEE967BF@microsoft.com... >I have a spread sheet of 47 rows. I need to multiply each row in column C >by > 25.378692%. I need the totals to appear in column D an...

Visual C++ .NET 2003 and XP Style #2
Hi, I want to know how to programmically incorporate the XP style windows (frame, buttons, etc.) to a project in Visual C++ .NET 2003. Beginning with Visual C++ .NET 2003, Microsoft has changed the window it incorporate window styles to a project. In older versions, an application gets the XP style under XP automatically. That is no longer true in version .NET 2003. I have read the solution at MSDN including the one with using TheApp.exe.manifest. However, the solution requires that the program must have the manifest file in the same directory as the EXE, even in final release. I ...

Can't send new mail message in Outlook 2007 b/c a dialog box is already open
Gurus, My Outlook 2007 had issue creating a "New" message complaining that a dialog box was already "Open". Outlook needed to be closed and re-opened, and the dialog box prompting for User Initials had to be acknowledged before the other dialog boxes were answered or Outlook would continually complain that a box was already "Open" when trying to send a new mail message. Anyone seen this? So many dialog prompts seem to launch when a new user starts OL 2007 such as the prompt for newsfeeds, Instant Search and the dialog to enter in user information that Ou...

dao select query problem in mfc C++
hi, i'm using dao for the first time to connect to a MS Access db. I've been following the MS example for a very basic query. This is the MS example: The following code shows the process of creating a recordset from a querydef: // pdb is a pointer to an open CDaoDatabase object try { // Construct the querydef CDaoQueryDef qd( pdb ); // Set up the SQL string and create the querydef CString strSQL = _T("SELECT [Company Name] FROM Publishers WHERE State = 'NY'"); qd.Create( _T("My Querydef"), strSQL ); // Construct a CDaoRec...

MFC String-table in C# ?
Hi, maybe a bit OT for this newsgroup, but I know that people here use C#, too. So, does anyone know how the MFC string-table technique map to C#/WinForm? I initially thought about using an integer->string map in C#, but is there some built-in mechanism to manage that? Thanks, Giovanni "Giovanni Dicanio" <giovanniDOTdicanio@REMOVEMEgmail.com> ha scritto nel messaggio news:ugqw1g7LJHA.1736@TK2MSFTNGP03.phx.gbl... > So, does anyone know how the MFC string-table technique map to C#/WinForm? ....after some web search, it seems that C# has a ResourceManager class ...

Convert IP Address to a hexadecimal
I know this is a long shot but is there anyone out there that knows ho to convert IP of the system to hexadecimal number -- eluehman ----------------------------------------------------------------------- eluehmann's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=1309 View this thread: http://www.excelforum.com/showthread.php?threadid=26622 > I know this is a long shot but is there anyone out there that knows how > to convert IP of the system to hexadecimal number? One way is to start with the four components in A1, B1, C1, and D1. (If they're in on...

Image quality when converting Publisher file to .pdf
I'm trying to convert a one page publication I created in Publisher into a ..pdf. I have one image - a logo - does not reproduce correctly in the .pdf. It always looks fuzzy, no matter what file format I use for the logo. The problem is worse if I use the pencil tool to eliminate the white background behind the logo, making the background transparent. Any suggestions would be appreciated. ...

in publisher 2000, how do I convert a standard 3 fold brochure in.
This is a simple question- but I would still like to use the wizard options, but just change the format so that the A4 page fold twic"e, instead of three times Open Publisher twice, open the brochure, select all, copy and paste to the other instance of Publisher. Adjust the elements... -- Mary Sauer MS MVP http://office.microsoft.com/ http://msauer.mvps.org/ news://msnews.microsoft.com "PG" <PG@discussions.microsoft.com> wrote in message news:74BC41AD-E6C5-42D4-9506-ABF225E15DA0@microsoft.com... > This is a simple question- but I would still like to use the wiza...

How do I convert the date from 1900's to 1800's?
I'm working on a spreadsheet in which I'm using dates from the 1800's. Is there a way to convert the date from 11/29/65 to 29 November 1865? I've tried to change it, but of course it goes to 1965 instead. In the meantime I've been changing each date manually, but I wanted to see if there was an easier and quicker way to convert the dates. Thanks! Hi Diana Try =MID(A1,4,2)&" "&CHOOSE(LEFT(A1,2),"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct&...

convert date to text month
i want to make a pivot table with the months as col headings. my raw data looks like this: col a col b col c col d date -- account cost center amt i have tried formatting col a as date (Jan 01, Feb - 01, etc) but when i try to put the data into a pivot table with the dates as the col heading, the pivot table ignores the Jan -01 format and puts each date as a col heading. i know that i could sort the data and manually put in "Jan" for all the dates between 1/1 and 1/31 but i have man...

cannot convert from 'class CPoint [2]' to 'const class CPoint [2]'
Hi i am getting the above compile error (C2440) and i can't work out why. This is what it involves class MyClass { protected: CPoint memberPoint[2]; } MyClass::MyFunction() { CPoint stackPoint[2]; stackPoint[0] = somePoint; stackPoint[1] = anotherPoint; memberPoint = stackPoint; } the above error occurs at the memberPoint = stackPoint line. I cant work out why i cannot assign 1 point array that is created on the stack and assigned values (stackPoint[2]) to another array that was created as a member array of the class. I didnt initialize the member point array, but i shouldnt have ...

Converting WDB file to MDB
How do I convert a Mocrosoft Works WDB file to an Access MDB file? I have Access 2003. Bizarre as it sounds, you can't. Try converting to an intermediary format that they both know, like csv or dbf, then importing into Access. -- Doug Steele, Microsoft Access MVP http://I.Am/DougSteele (no e-mails, please!) "Ray Frost" <anonymous@discussions.microsoft.com> wrote in message news:148a01c48c3f$6f67fa10$a601280a@phx.gbl... > How do I convert a Mocrosoft Works WDB file to an Access > MDB file? I have Access 2003. What is csv and dbf and how do I convert from wo...

Converting messages and contacts
How can I convert all of my old Entourage 2004 messages and contacts to a csv or to raw text files? I only want to have easy access to very old emails. If I use a Spotlight search, I just want to view the messages in a text file or open a database to view the message and/or contact information. I always used text format for my incoming and outgoing messages and never HTML. Is there an app that I can just drag my Main Identity folder into it and Presto Chango...it is now all in a format that I can read in text or in open in Excel or import into Excel or Open Office database? Or even...

Microsoft Visual C++ Runtime Library: Runtime Error!
Product: Microsoft Money 2001 Operating System: Microsoft Windows XP Receive error when try to open Microsoft Money: Microsoft Visual C++ Runtime Library Runtime Error! Program C:\Program Files\Microsoft Money\System\msmoney.exe Was there a question here? You might want to read http://umpmfaq.info/faqdb.php?q=131. "Dave" <ddoroquez@hotmail.com> wrote in message news:5ec301c42ddf$ece39030$a301280a@phx.gbl... > Product: Microsoft Money 2001 > Operating System: Microsoft Windows XP > > Receive error when try to open Microsoft Money: > > Microsoft Visual C++...

How do I modify the value of a checkbox in a word document using C
Hi all, I am a bit of a newbie at C# and am trying to set up a document that I will be modifying based on values from a database. I am using a word document, because it is the "official" reporting form required by my company. I can open the document, and write to bookmarks that I am setting in each table field, but I need to know how to manipulate a checkbox. Can anyone help me? I have edited this post because I have added some other code. The document file I am using is a document that has everything inside a table. I have bookmarked all the text locations, and...

How to convert MOD to MOV (QuickTime) without interlaces?
Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel How to convert MOD to MOV (QuickTime) without interlaces? <br><br>Have you ever been troubled by how to convert MOD shot by your camcorder to MOV for QuickTime on Mac? Or do you want to watch MOD files in QuickTime or other media players? The tutorial will show you an easy and fast method to convert MOD to MOV on Mac via a reliable Mac MOD Converter � Moyea MOD Converter for Mac. <br><br>[b]Preparation[/b]: Free download Moyea MOD Converter here, install and run it. <br><br>[b]Step 1: Click ...

XML Insert data C# ASP.net 2.0 Need to know how to insert by speci
Hello, I have the following XML Schema: <?xml version="1.0" encoding="utf-8"?> <Schedule> <Day Name="Monday"> </Day> <Day Name="Tuesday"> </Day> <Day Name="Wednesday"> </Day> <Day Name="Thursday"> </Day> <Day Name="Friday"> </Day> <Day Name="Saturday"> </Day> <Day Name="Sunday"> </Day> </Schedule> I want users to be able to input the following tiomeblocks for a scheduling s...