how to get an utc timestamp? (TW5)

136 views
Skip to first unread message

chaonuo

unread,
Aug 22, 2015, 8:47:13 AM8/22/15
to TiddlyWiki
hi everybody,

For my purpose I need to set a "date" field for my agenda Tiddlers and enter a timestamp as its value. Since the "created" and "modified" are in UTC time, I think it is more properly if my "date" field also uses UTC time. At present I use the macro <<now>> to generate a current local timestamp (in my case UTC+8) and then edit it by hand. 

How does one get an UTC timestamp instead? Is there an equivalent macro for generating UTC timestamps?

thanks !

chaonuo

unread,
Aug 22, 2015, 11:11:43 PM8/22/15
to TiddlyWiki
I have just managed to solve this problem. so I share it here, maybe someone else can also make use of it.

1. clone the javasript $:/core/modules/macros/now.js to $:/USERNAME/modules/macros/utc.js

2. change the line
exports.name = "now";
to
exports.name = "utc";
this will be the name of the new macro


and change the macro part to:
/*
Run the macro
*/
exports.run = function(format) {
        var now = new Date(); 
        return $tw.utils.formatDateString(new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),  now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds()),format || "YYYY0MM0DD0hh0mm0ss");
};

now I can use <<utc>> and get an utc timestamp.

I don't know javascript at all, I just searched on the web and happened to find a workable solution. probably there are better ways to do this.

for other beginner like me: you need to refresh the TW5 before the javascript macro can be used.

I wish all have a nice day!

chaonuo
Reply all
Reply to author
Forward
0 new messages