Jump to content

Date Convert in Registry


Recommended Posts

Hi,

I am trying to convert the Date format of a file path in registry from yyyy/mm/dd to mm/dd/yyyy.

As per the below code, "InstallDate" shows in registry as in yyyy/mm/dd format. is there any ways I can change the format in mm/dd/yyyy and display it.

$File = "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Updates\KBXXXXXX"
    $Date = RegRead($File, "InstallDate")

    MsgBox(0, "", $Date)

 

Link to comment
Share on other sites

This should work :

$File = "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Updates\Microsoft .NET Framework 4 Client Profile\KB2468871"
$Date = StringRegExpReplace( RegRead($File, "InstalledDate"), "(\d{4})/(\d{2})/(\d{2})", "$2/$3/$1")

MsgBox(0, "", $Date)

 

Link to comment
Share on other sites

thank you @jguinch

I changed it to "$3/$2/$1" to get the mm/dd/yyyy format.

just one more help needed if you can please.

I am trying to get the date difference between the $Date from today's Date. But as per below I always get 0.

If I look for $Date it shows me in correct format, as mm/dd/yyyy and the same with $Nowdate. But if I am looking for $DateDiff it comes as 0 instead of the the actual difference in days.

$File = "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Updates\Microsoft .NET Framework 4 Client Profile\KB2468871"
$Date = StringRegExpReplace( RegRead($File, "InstalledDate"), "(\d{4})/(\d{2})/(\d{2})", "$3/$2/$1")

$Nowdate = _NowDate()
$DateDiff = _DateDiff('d', $Date, $Nowdate)
MsgBox(0, "", $DateDiff)

 

Edited by sunshinesmile84
Link to comment
Share on other sites

On my computer that registry key doesn't have an InstalledDate entry, it just has "ThisVersionInstalled" in it.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

This is an update and if you don't have it installed then it won't be in the registry...

Or you can look at the below location if you have it and that might show you in yyyy/mm/dd or mm/dd/yyyy

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Updates\Microsoft Visual C++ 2010  x64 Redistributable - 10.0.40219\SP1\KB2565063

Link to comment
Share on other sites

I didn't say the key wasn't in my registry, I said the value "InstalledDate" isn't in that key. "ThisVersionInstalled" is set to Y so the update is installed, but there's no install date in there on my computer.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Because _DateDiff will only accept dates in the format of yyyy then mm then dd, It will not accept a date format of mm/dd/yyyy.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

It seems that the installation date value is not always present, so another way is to directly retrieve the key date (last written time) instead :

#Include <Date.au3> ; needed for _RegEnumKeyEx

$sDate = _RegGetTime("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Updates\Microsoft Visual C++ 2010  x64 Redistributable - 10.0.40219\SP1")
If Not @error Then
    $iDateCalc  = _DateDiff('D', $sDate, _NowCalc())
    ConsoleWrite("subkey last written on " & $sDate & "  /  " & $iDateCalc & " days ago")
EndIf







Func _RegGetTime($sKey)
    Local $aInfos, $aSearch = StringRegExp($sKey, "(.+)\\([^\\]+)\\?$", 1)
    If @error Then Return SetError(1, 0, 0)
    
    Local $i = 0
    While 1
        $i += 1
        $aInfos = _RegEnumKeyEx($aSearch[0], $i)
        If @error Then ExitLoop
        
        If $aInfos[0] = $aSearch[1] Then Return $aInfos[1]
    WEnd
    Return SetError(2, 0, 0)
EndFunc



Func _RegEnumKeyEx($sKey, $iInstance)
    If NOT IsDeclared("KEY_WOW64_32KEY") Then Local Const $KEY_WOW64_32KEY = 0x0200
    If NOT IsDeclared("KEY_WOW64_64KEY") Then Local Const $KEY_WOW64_64KEY = 0x0100
    If NOT IsDeclared("KEY_ENUMERATE_SUB_KEYS") Then Local Const $KEY_ENUMERATE_SUB_KEYS = 0x0008
 
    If NOT IsDeclared("tagFILETIME") Then Local Const $tagFILETIME = "struct;dword Lo;dword Hi;endstruct"
 
    Local $iSamDesired = $KEY_ENUMERATE_SUB_KEYS
 
    Local $iX64Key = 0, $sRootKey, $aResult[2]
 
    Local $sRoot = StringRegExpReplace($sKey, "\\.+", "")
    Local $sSubkey = StringRegExpReplace($sKey, "^[^\\]+\\", "")
 
    $sRoot = StringReplace($sRoot, "64", "")
    If @extended Then $iX64Key = 1
 
    If NOT IsInt($iInstance) OR $iInstance < 1 Then Return SetError(2, 0, 0)
 
    Switch $sRoot
        Case "HKCR", "HKEY_CLASSES_ROOT"
            $sRootKey = 0x80000000
        Case "HKLM", "HKEY_LOCAL_MACHINE"
            $sRootKey = 0x80000002
        Case "HKCU", "HKEY_CURRENT_USER"
            $sRootKey = 0x80000001
        Case "HKU", "HKEY_USERS"
            $sRootKey = 0x80000003
        Case  "HKCC", "HKEY_CURRENT_CONFIG"
            $sRootKey = 0x80000005
        Case Else
            Return SetError(1, 0, 0)
    EndSwitch
 
    If StringRegExp(@OSArch, "64$") Then
        If @AutoItX64 OR $iX64Key Then
            $iSamDesired = BitOR($iSamDesired, $KEY_WOW64_64KEY)
        Else
            $iSamDesired = BitOR($iSamDesired, $KEY_WOW64_32KEY)
        EndIf
    EndIf
 
    Local $aRetOPen = DllCall('advapi32.dll', 'long', 'RegOpenKeyExW', 'handle', $sRootKey, 'wstr', $sSubKey, 'dword', 0, 'dword', $iSamDesired, 'ulong_ptr*', 0)
    If @error Then Return SetError(@error, @extended, 0)
    If $aRetOPen[0] Then Return SetError(10, $aRetOPen[0], 0)
 
    Local $hKey = $aRetOPen[5]
 
    Local $tFILETIME = DllStructCreate($tagFILETIME)
    Local $lpftLastWriteTime = DllStructGetPtr($tFILETIME)
 
    Local $aRetEnum = DllCall('Advapi32.dll', 'long', 'RegEnumKeyExW', 'long', $hKey, 'dword', $iInstance - 1, 'wstr', "", 'dword*', 255, 'dword', "", 'ptr', "", 'dword', "", 'ptr', $lpftLastWriteTime)
    If Not IsArray($aRetEnum) OR $aRetEnum[0] <> 0 Then Return SetError( 3, 0, 1)
 
    Local $tFILETIME2 = _Date_Time_FileTimeToLocalFileTime($lpftLastWriteTime)
    Local $localtime = _Date_Time_FileTimeToStr($tFILETIME2, 1)
 
    $aResult[0] = $aRetEnum[3]
    $aResult[1] = $localtime
 
    Return $aResult
EndFunc

 

Edited by jguinch
Link to comment
Share on other sites

I don't get it... if I am using Datediff for the Filegettime instead of Regread it works absolutly correct.

File Get time is also in the format mm/dd/yyyy.

​FileGetTime returns an array, or a string if you choose 1 for the Format parameter. The string returned when format is one is in the form of YYYYMMDDHHMMSS as noted in the help file. I don't know what function you're using to get the time in mm/dd/yyyy, but it's not FileGetTime.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

@BrewManNH

you are correct. Below is just an example that I am using for Filegettime and that is working fine. Please note that this is just an example... Not the actual file path.

Now I wanted the same with Registry entry to get the Date Diff.

 

; FileGetTime
    $FileDate = FileGetTime("C:\Program Files (x86)\Microsoft .NET Framework 4 Client Profile\KB2468871", 0)
            $date = $FileDate[0] & "/" & $FileDate[1] & "/" & $FileDate[2]
            $Nowdate = _NowCalc()
            $iDateDif = _DateDiff('d', $date, $Nowdate)
            MsgBox(0, "", $iDateDif)

 

;RegGetTime
$File = "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Updates\Microsoft .NET Framework 4 Client Profile\KB2468871"
$Date = StringRegExpReplace( RegRead($File, "InstalledDate"), "(\d{4})/(\d{2})/(\d{2})", "$3/$2/$1")

$Nowdate = _NowDate()
$DateDiff = _DateDiff('d', $Date, $Nowdate)
MsgBox(0, "", $DateDiff)

I didn't think that this would be so difficult. :)

If you can help with the above explanation, would really appreciate.

Edited by sunshinesmile84
Link to comment
Share on other sites

What does the RegRead function return from that key/value, without the unnecessary StringRegExpReplace? What exactly does the date look like when you use RegRead on it?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

What does the RegRead function return from that key/value, without the unnecessary StringRegExpReplace? What exactly does the date look like when you use RegRead on it?

​that returns me in YYYY\DD\MM format.

Link to comment
Share on other sites

So why are you using the StringRegExReplace to convert it to an unsupported format for the _DateDiff function?

​that returns me in YYYY\DD\MM format.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

So why are you using the StringRegExReplace to convert it to an unsupported format for the _DateDiff function?

​StringRegReplace was suggested above... as per my earlier query for 1st changing the format to mm/dd/yyyy and then I wanted this registry value date as difference with _NowDate() to give me the result.

Just an example, if Registry value date is given as 2015/22/03, using StringRegReplace will change the format to 03/22/2015 and to see the difference in number of days from current date, I was using _DateDiff.

As I said earlier, for Regitry value dates it might be little diff way than Filegettime.

 

So, if there is a better way with you please share.

I would need the date from yyyy/dd/mm to be changed to mm/dd/yyyy and then showing the date difference with current date.

 

just to help you again, this is my code without any stringregreplace. InstallDate has the date format in it as 2015/22/03

$File = "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Updates\Microsoft .NET Framework 4 Client Profile\KB2468871"
$Date = RegRead($File, "InstalledDate")
Edited by sunshinesmile84
Link to comment
Share on other sites

I figured it out at last....

My registry date is in YYYY/MM/DD format so I used the below code and it gave me the date diff.

$File = "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Updates\Microsoft .NET Framework 4 Client Profile\KB2468871"
$Date = RegRead($File, "InstalledDate")
    $DateDiff = _DateDiff('d', $Date, _NowCalcDate())

    MsgBox(0, "", $DateDiff)

 

Thank you all for helping here. :)

 

Link to comment
Share on other sites

Yes, that's what I told you to do, get rid of the SRER that you didn't need.

Your original question never specified that you needed to convert a date to use with DateDiff. You added that part after your original question was answered. If you had read the help file and had seen the formats of dates that _DateDiff accepts in the first place, you would not have needed to ask how to change it.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...