[SOLVED] Code snippet for timestamp Now()

To discuss development of addons / skins / customization of MediaMonkey.

Moderators: jiri, drakinite, Addon Administrators

Andre_H
Posts: 415
Joined: Thu Jan 21, 2021 2:04 pm
Location: Germany

[SOLVED] Code snippet for timestamp Now()

Post by Andre_H »

Hi guys,

maybe someone can link me to a codesample or something, my bumbling attempts to set a value and googling the syntax for it dont work. I simply want to check some fields and write the value of "now()" to the "track.lastTimePlayed"-Field, but so far I only get invalid values (i tried "Date.Now()" and "app.utils.dateTime2Timestamp(Date.now())" in some different variants, they seem to gives invalid values, or i'm doing it wrong ...

Code: Select all

		
  let varrating = track.rating/20;
  if(varrating > 1 && track.playCounter === 0) {	
	track.playCounter = 1;     		// works!
	track.lastTimePlayed=Date.now(); 	// what syntax has to be set here?
	track.commitAsync();
	};
Thank you!
Last edited by Andre_H on Wed Jan 05, 2022 12:31 pm, edited 1 time in total.
- MMW 5.0.4.2690 (non-portable, shared DB & files) on Windows 2016 # only essential addons # my 24/7 media server
- MMW MMW 5.0.4.2690 (non-portable, shared DB & files) on Windows 10 # playing, testing skins & addons # my desktop app
- MMA Pro (2.0.0.1063) on Android 10, 11, 12 Phones & Tabs # WiFi Sync # playing

- MP3Tag, MP3Diags, MP3DirectCut, IrfanView
Barry4679
Posts: 2398
Joined: Fri Sep 11, 2009 8:07 am
Location: Australia
Contact:

Re: Code snippet for timestamp Now()

Post by Barry4679 »

Andre_H wrote: Mon Jan 03, 2022 12:56 pm Hi guys,

maybe someone can link me to a codesample or something, my bumbling attempts to set a value and googling the syntax for it dont work. I simply want to check some fields and write the value of "now()" to the "track.lastTimePlayed"-Field, but so far I only get invalid values (i tried "Date.Now()" and "app.utils.dateTime2Timestamp(Date.now())" in some different variants, they seem to gives invalid values, or i'm doing it wrong ...
if you are going to write these values into the database, you could let sql do the work for you.
See here for SQLITE builtin date and time functions.

eg:

Code: Select all

select julianday('now','localtime')
or

Code: Select all

select strftime('%Y-%m-%d %H:%M:%S','now','localtime')
Want a dark skin for MM5? This is the one that works best for me .. elegant, compact & clear.
TIV73
Posts: 229
Joined: Sat Nov 12, 2011 1:31 pm

Re: Code snippet for timestamp Now()

Post by TIV73 »

Try one of these

Code: Select all

track.lastTimePlayed =  app.utils.myDecodeDate((new Date()).toISOString().substring(0, 10))
track.lastTimePlayed = app.utils.myDecodeDate(app.utils.dateTime2Timestamp((new Date())))
Andre_H
Posts: 415
Joined: Thu Jan 21, 2021 2:04 pm
Location: Germany

Re: Code snippet for timestamp Now()

Post by Andre_H »

@Barry: Thanks for the suggestion, but I don't know how to initiate (automated) SQL updates. The code excerpt shown is part of a larger script (see https://www.mediamonkey.com/forum/viewtopic.php?t=98235), and much more than what is shown in it then exceeds my capabilities.

@TIV73: I'm not sure what exactly isn't working, but nothing is saved. I've tried both variants, I don't get an error message, but nothing is written in the "LastTimePlayed" field. Script itself runs, the playCounter is set after.
- MMW 5.0.4.2690 (non-portable, shared DB & files) on Windows 2016 # only essential addons # my 24/7 media server
- MMW MMW 5.0.4.2690 (non-portable, shared DB & files) on Windows 10 # playing, testing skins & addons # my desktop app
- MMA Pro (2.0.0.1063) on Android 10, 11, 12 Phones & Tabs # WiFi Sync # playing

- MP3Tag, MP3Diags, MP3DirectCut, IrfanView
TIV73
Posts: 229
Joined: Sat Nov 12, 2011 1:31 pm

Re: Code snippet for timestamp Now()

Post by TIV73 »

Please disregard the previous snippet. While the code posted works for some fields using dates, the lastTimePlayed field wants a full timestamp with time, not a date. Also make sure to lock the tracks you are trying to update, just writing the properties won't work.

Here's the full snippet:

Code: Select all

var tracklist = uitools.getSelectedTracklist();
tracklist.modifyAsync(function () {
	var track = tracklist.getValue(0);
	track.lastTimePlayed = convertUnixToMSTimestamp(Date.now())
});
edit:
btw, if you need to figure out what kind of data a specific field wants you can either check the track property in the API reference or have a look the tracks of a tracklist using the asJson property of a tracklist which will return the values of all fields.
Ludek
Posts: 4945
Joined: Fri Mar 09, 2007 9:00 am

Re: Code snippet for timestamp Now()

Post by Ludek »

Hi,
I think that something like this should work:

Code: Select all

track.lastTimePlayed_UTC = app.utils.timestamp2DateTime(  new Date().toISOString());
i.e. the param into timestamp2DateTime needs to be in the ISO form 'YYYY-MM-DD HH:MM:SS' but seeing the code now it should accept also the '2022-10-05T14:48:00.000Z' variants, i.e. every ISO 8601 variant longer than 18 chars.

EDIT2: Also seeing that toISOString is UTC, so you must use track.lastTimePlayed_UTC

EDIT3: Also, the date properties (like fileModified, dateAdded, lastTimePlayed) and its UTC variants aren't documented at https://www.mediamonkey.com/docs/api/classes/Track.html
We should add them to the docs! I'll do it.

Thanks for feedback and for creating addons!
Andre_H
Posts: 415
Joined: Thu Jan 21, 2021 2:04 pm
Location: Germany

Re: Code snippet for timestamp Now()

Post by Andre_H »

Thank you, guys, works now! :wink:
- MMW 5.0.4.2690 (non-portable, shared DB & files) on Windows 2016 # only essential addons # my 24/7 media server
- MMW MMW 5.0.4.2690 (non-portable, shared DB & files) on Windows 10 # playing, testing skins & addons # my desktop app
- MMA Pro (2.0.0.1063) on Android 10, 11, 12 Phones & Tabs # WiFi Sync # playing

- MP3Tag, MP3Diags, MP3DirectCut, IrfanView
sonos
Posts: 170
Joined: Wed Aug 07, 2013 11:54 am

Re: [SOLVED] Code snippet for timestamp Now()

Post by sonos »

Andre_H wrote
[Solved] Code snippet for timestamp Now()
Hi Andre_H
How did you achieve this? Did you update the lastTimePlayed field via SQL after listening a Song?
Andre_H
Posts: 415
Joined: Thu Jan 21, 2021 2:04 pm
Location: Germany

Re: [SOLVED] Code snippet for timestamp Now()

Post by Andre_H »

Hi sonos,

thats what i do (as part of a larger script):

Code: Select all

actions.saveStatistics = {
    title: _('Speichere Statistik-Daten in CustomFields ...'),
    hotkeyAble: false,
    icon: 'saveStatistics',
    disabled: uitools.notMediaListSelected,
    visible: window.uitools.getCanEdit,
    execute: async function () {
        var list = await uitools.getSelectedTracklist().whenLoaded();
        if (list.count === 0) {
            return;
        }
        list.forEach(function(track) {

           var modified = false;
	   let varrating = track.rating/20;

		////////////////////////////////////////////////////////////////////////////////////
		// PLAYCOUNTER & LASTPLAYED: "PlayCounter"=1 & "LastPlayed"=Now(), wenn "PlayCounter"=0 und "Rating">1 ...
		if(track.playCounter === 0 && varrating > 1) {
		   track.playCounter = 1;
		   track.lastTimePlayed_UTC = app.utils.timestamp2DateTime(new Date().toISOString());
		   // track.commitAsync(); 
		   modified = true;
		};


           if (modified)
		track.commitAsync(); 

        });       
    }
}

window._menuItems.editTags.action.submenu.push({
        action: actions.saveStatistics,
        order: 10,
        grouporder: 10
});
... so, no direct SQL statements. the script runs on every change of the track as well as on right-click.

if you want the whole script (all the files needed to run it), just PM me.
- MMW 5.0.4.2690 (non-portable, shared DB & files) on Windows 2016 # only essential addons # my 24/7 media server
- MMW MMW 5.0.4.2690 (non-portable, shared DB & files) on Windows 10 # playing, testing skins & addons # my desktop app
- MMA Pro (2.0.0.1063) on Android 10, 11, 12 Phones & Tabs # WiFi Sync # playing

- MP3Tag, MP3Diags, MP3DirectCut, IrfanView
Post Reply