How to parse Chrome bookmarks "date_added" value to a Date
Asked Answered
T

6

13

The Chrome bookmarks file is JSON which contains a "date_added" value that represents a particular date and time, e.g.

{
 "checksum": "05b8bba8b5f0e9ad1cc8034755557735",
 "roots": {
    "bookmark_bar": {
       "children": [ {
          "children": [ {
             "date_added": "13170147422089597",
             "id": "121",
             "name": "NativeScript: Getting Started Guide",
             "type": "url",
             "url": "https://docs.nativescript.org/tutorial/chapter-0"
          } ],
...

I have tried treating the value as nanoseconds and passing to the Date constructor:

new Date(13170147422089597 / 1000); // 2387-05-07T06:17:02.089Z

but that doesn't seem correct.

How should the value "13170147422089597" be converted to a Date or date string?

Temple answered 14/7, 2018 at 22:20 Comment(1)
@MarkMeyer—I found similar questions for Python, PHP, C# etc. but nothing for javascript. :-)Temple
T
17

The Chrome bookmarks time value is microseconds from an epoch of 1601-01-01T00:00:00Z. To convert to a Date:

  1. Divide by 1,000 to get milliseconds
  2. Adjust to an epoch of 1970-01-01T00:00:00Z
  3. Pass the resulting value to the Date constructor

E.g.

var timeValue = '13170147422089597';
new Date(Date.UTC(1601,0,1) + timeValue / 1000); // 2018-05-07T06:17:02.089Z

Storing the value Date.UTC(1601,0,1) as a constant (-11644473600000) and converting to a function gives:

function chromeTimeValueToDate(tv) {
  var epoch = -11644473600000;
  return new Date(epoch + tv / 1000);
}

// Example
['13170147422089597',
 '13150297844686316',
 '13115171381595644'].forEach( tv => {
   console.log(chromeTimeValueToDate(tv))
});
Temple answered 14/7, 2018 at 22:20 Comment(4)
Should be microseconds instead of nanoseconds. ^_^Bergman
@CharlesJie—oh yes, how did I miss that?Temple
Why 1601? See #10850217Pentahedron
Weirdly, timings are still off. I just added a url bookmark, inside a newly created folder, and then logged the items' date as described here, sorted by latest, and the two items (the folder and the url) appeared ~40 mins apart (I created them at the same time!), and also ~14 (& 40+14) mins in the past.Strigil
B
7

It's WebKit/Chrome Timestamp, representing microseconds since 1601/1/1 UTC.

It comes from Windows NT Timestamp (as 100-nanoseconds since 1601/1/1 UTC), available as FileTime structure.

There is an online WebKit/Chrome Timestamp Converter in https://www.epochconverter.com/webkit

which provides its code there as well.

However the code is in Python2, so I transcribe it to Python3 as below:

import datetime
def date_from_webkit(webkit_timestamp):
    epoch_start = datetime.datetime(1601,1,1)
    delta = datetime.timedelta(microseconds=int(webkit_timestamp))
    print(epoch_start + delta) # py3 requires () for print

date_from_webkit(int(input('Enter a Webkit timestamp to convert: '))) # py3 integrates raw_input() and input() into input()
Bergman answered 15/9, 2019 at 22:46 Comment(1)
⬆ for the added detail explaining the origin of the bizarre epoch.Vermicide
G
1

I like to prototype these things in a spreadsheet and then it should be trivial to code the coded formula in whichever language you happen to be working with.

Formula = "2001-1-1" + A2/24/60/60/1000000 - 400*365.25+3+7/24

Explanation:

  • Starting with an arbitrary date that Excel (what I'm using as a spreadsheet) can handle... Jan1, 2001
  • Add the bookmark's data value [date_added] or [last_visited_desktop] or [date_modified] (in cell A2 in my formula above) in terms of days (24 hours by 60 minutes by 60 seconds by a millionth of a second
  • subtract 400 years in terms of days (that's 400 years including leap year days and 7 hours worth of clock adjustments since by 2001 date above, because somehow COBOL developers got stuck on year 1601, which became year 1 for them, so they made that their 'epoch'. Funny how things get started, eh?)

For example, Chrome's Bookmark [date_added] value is "13190650905699900", so: "2001-1-1"+ 13190650905699900/24/60/60/1000000 - 400*365.25+3+7/24 yields 12/30/2018 8:41:46pm

You can find the Bookmarks file here:

%LOCALAPPDATA%\Google\Chrome\User Data\Default\Bookmarks

as of current version of Chrome (v71)

PS - Window Time utility can do it for you too if all you want is a quick one-time conversion: type

w32tm.exe /ntte 131906509056999000

at a command prompt - notice I had to add a '0' (or multiply by 10).

Gauldin answered 30/12, 2018 at 14:11 Comment(0)
E
1

And, just to save somebody time... (obvious in retrospect)... The bookmarks export file is in a different format, the date value ADD_DATE in the exported bookmarks file is 10 characters and in the JSON file the date_added field is 17 characters in length.

Dividing date_added by ADD_DATE does not give an immediately useful answer.

Expressman answered 20/6, 2019 at 19:2 Comment(1)
Hi, welcome to stack overflow. Explain why this is an answer to the question.Spend
R
1

Just for those who need the same algorithm in .NET / PowerShell, here is the implementation in PowerShell (the .NET version should be then pretty straightforward):

$timeValue = 13268127530603048
$epoch = (New-Object DateTime(1601, 1, 1)).Ticks
$utcTime = New-Object DateTime(($epoch + $timeValue * 10), [System.DateTimeKind]::Utc)
$utcTime.ToLocalTime()
Retrogression answered 14/6, 2021 at 8:29 Comment(0)
Y
0

Here's a handy online converter for Chrome and Apple-WebKit data/time stamp to human readable format. Just copy and paste the long date/time stamp, and press the Convert button.

https://www.epochconverter.com/webkit

Here's the converter in Python script.

import datetime
def date_from_webkit(webkit_timestamp):
    epoch_start = datetime.datetime(1601,1,1)
    delta = datetime.timedelta(microseconds=int(webkit_timestamp))
    print epoch_start + delta

inTime = int(raw_input('Enter a Webkit timestamp to convert:'))
date_from_webkit(inTime)
Ygerne answered 10/10, 2023 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.