Convert text into a date

I use Simple RSS to retrieve articles from some feeds, and this works well.
The pubDate of an item looks like this: Wed, 08 Jan 2025 10:00:00 Z
I can use split and text concatenation to convert the date-string into this 2025-Jan-08

I want the “articles” sorted by pubDate this means I have to replace Jan into 01, Apr into 04 …

I was thinking of defining a key:value object and then use
object.split(published, " ")[2] to map the Month’s shortname to a “two-digit” string.

Unfortunately, I have no idea where to define the key:value object and how to pass the “Jan”-key to the object.

To be honest, I have no idea, if this is a smart move, if this is much more simple, or if it’s not possible at all.

This is what my dataview-code looks like at the moment:

dataview
TABLE 
split(published, " ")[3] + "-" + split(published, " ")[2] + "-" + split(published, " ")[1]  as "pubDate"

from "SimpleRSS/MicrosoftFabricBlog"

SORT published ascending
LIMIT 20

I’m unsure if core DataView uses the key: value objects. It could be that you’ll need to use it with JS - dataviewjs. Try something like this:

const monthMap = {
  Jan: "01", Feb: "02", Mar: "03", Apr: "04",
  May: "05", Jun: "06", Jul: "07", Aug: "08",
  Sep: "09", Oct: "10", Nov: "11", Dec: "12"
};

dv.table(
  ["pubDate", "Title"],
  dv.pages('"SimpleRSS/MicrosoftFabricBlog"')
    .map(p => {
      const parts = p.published.split(" ");
      const day = parts[1];
      const month = monthMap[parts[2]];
      const year = parts[3];
      const formattedDate = `${year}-${month}-${day}`;
      return { pubDate: formattedDate, Title: p.file.name };
    })
    .sort(p => p.pubDate, 'asc')
);

You’ll probably need to test it and adapt it to your needs.

Cheers, Marko :nerd_face:

Not sure if this work, no mapping … try it out:

dv.table(
  ["Formatted Date", "Title"],
  dv.pages('"SimpleRSS/MicrosoftFabricBlog"')
    .map(p => {
      const date = new Date(p.published);
      const formattedDate = `${date.getFullYear()}-${padleft((date.getMonth() + 1).toString(), 2, "0")}-${padleft(date.getDate().toString(), 2, "0")}`;
      return { "Formatted Date": formattedDate, "Title": p.file.name };
    })
    .sort(p => p["Formatted Date"], 'asc')
);

Cheers, Marko :nerd_face:

Hey Marko,

your first snippet returns this error:

Evaluation Error: TypeError: row.map is not a function
    at eval (plugin:dataview:15613:96)
    at Array.map (<anonymous>)
    at Proxy.map (plugin:dataview:8269:39)
    at b$1.TableGrouping [as constructor] (plugin:dataview:15613:64)
    at b$1.B$2 [as render] (plugin:dataview:15050:8935)
    at L$1 (plugin:dataview:15050:6523)
    at P$1 (plugin:dataview:15050:2215)
    at L$1 (plugin:dataview:15050:6742)
    at P$1 (plugin:dataview:15050:2215)
    at L$1 (plugin:dataview:15050:6742)

And the 2nd snippet returns this error:

Evaluation Error: ReferenceError: padleft is not defined
    at eval (eval at <anonymous> (plugin:dataview), <anonymous>:1:200)
    at Array.map (<anonymous>)
    at Proxy.map (plugin:dataview:8269:39)
    at eval (eval at <anonymous> (plugin:dataview), <anonymous>:1:123)
    at DataviewInlineApi.eval (plugin:dataview:18885:16)
    at evalInContext (plugin:dataview:18886:7)
    at asyncEvalInContext (plugin:dataview:18896:32)
    at DataviewJSRenderer.render (plugin:dataview:18922:19)
    at DataviewJSRenderer.onload (plugin:dataview:18464:14)
    at e.load (app://obsidian.md/app.js:1:1248334)

Now I’m wondering if I miss something more general, like copy some JS files somewhere or enable as JS setting in Obsidian’s settings?

Let’s first check this …

… do you have this enabled in Dataview settings?

Cheers, Marko :nerd_face:

Yes, this is enabled.

OK, I see what can be wrong … I forgot how functionas work in DataViewJS. Try this:

dv.table(
  ["Formatted Date", "Title"],
  dv.pages('"SimpleRSS/MicrosoftFabricBlog"')
    .map(p => {
      const date = new Date(p.published);
      const formattedDate = `${date.getFullYear()}-${dv.func.padleft((date.getMonth() + 1).toString(), 2, "0")}-${dv.func.padleft(date.getDate().toString(), 2, "0")}`;
      return { "Formatted Date": formattedDate, "Title": p.file.name };
    })
    .sort(p => p["Formatted Date"], 'asc')
);

Cheers, Marko :nerd_face:

Unfortunately this:

Evaluation Error: TypeError: row.map is not a function
    at eval (plugin:dataview:15613:96)
    at Array.map (<anonymous>)
    at Proxy.map (plugin:dataview:8269:39)
    at b$1.TableGrouping [as constructor] (plugin:dataview:15613:64)
    at b$1.B$2 [as render] (plugin:dataview:15050:8935)
    at L$1 (plugin:dataview:15050:6523)
    at P$1 (plugin:dataview:15050:2215)
    at L$1 (plugin:dataview:15050:6742)
    at P$1 (plugin:dataview:15050:2215)
    at L$1 (plugin:dataview:15050:6742)

This

dv.table(
["File", "Published"], 
dv.pages('"SimpleRSS/MicrosoftFabricBlog"')
.map(i => [i.file.name, i.published]))

tells me at least that it is working as this returns the below image, of course not sorted

Now I have to figure how this inline function works to transform the published string

1 Like

It’s a bit of guessing, but let’s try this one…

dv.table(
  ["File", "Published"],
  dv.pages('"SimpleRSS/MicrosoftFabricBlog"')
    .map(p => {
      const date = new Date(p.published);
      const formattedDate = `${date.getFullYear()}-${dv.func.padleft((date.getMonth() + 1).toString(), 2, "0")}-${dv.func.padleft(date.getDate().toString(), 2, "0")}`;
      return [p.file.name, formattedDate]; // Return an array instead of an object
    })
    .sort(p => p[1], 'asc') // Sort by the second column (formatted date)
);

Cheers, Marko :nerd_face:

1 Like

Brilliant!

Thank you!

If you want you could do this in pure Dataview to, using object(). Given the month in month you could do:

FLATTEN object(
  "Jan", "01",
  "Feb", "02", 
  ...
  "Dec", "12")[month] as monthNo

So no problem doing a dictionary in ordinary Dataview, no need for dataviewjs here, unless you want to go down that road.

1 Like

Hey holroy,

can you please provide a complete dataview query, I was looking at flatten, but did not get the syntax correct.
Also i do not know how to make month dependent of the pubDate string value.

This is one step further on building my homepage …

A more complete example:

```dataview
TABLE pubDate, typeof(pubDate)
FROM "SimpleRSS/MicrosoftFabricBlog"

FLATTEN list(split(published, " ")) as pubParts
FLATTEN object(
  "Jan", "-01-",
  "Feb", "-02-", 
  "Mar", "-03-",
  "Apr", "-04-",
  "May", "-05-", 
  "Jun", "-06-",
  "Jul", "-07-",
  "Aug", "-08-",
  "Sep", "-09-",
  "Oct", "-10-",
  "Nov", "-11-",
  "Dec", "-12-")[pubParts[2]] as monthPart
FLATTEN date(pubParts[3] + monthPart + pubParts[1]) as pubDate

SORT pubDate ascending
LIMIT 20
```

Untested, but hopefully correct :smiley:

2 Likes

Works like a charm, and I will use this approach as it corresponds more to my current learning. I realized that the dataviewjs approach does not allow opening the note.

2 Likes