Sure; I hope you don't mind me pasting the whole code in here (what's the 
best way to share macros on this group btw?)

I'm just starting up with js macros for TW so this one is not perfect 
either. Anyway, any improvement ideas are welcome.

Thanks,
Hubert

/*\
title: $:/_Macros/DaysDiff.js
type: application/javascript
module-type: macro
author: hubertgk

Macro to return an optionally fractional number of days between now and a 
specified date and, optionally, time.

Required input:
<<DaysDiff YYYYMMDDHHMM>>

HH and MM are optional, when skipped then start of day (midnight) is assumed.

\*/
(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

/*
Information about this macro
*/

exports.name = "DaysDiff";

exports.params = [
        {name: "target_date"}
];

/*
Run the macro
*/

exports.run = function(target_date) {

        //parse the target date YYYYMMDDHHMM into discrete chunks 
(year/month/day/hour/minute)

    var input_year = target_date;
    var input_year = target_date.substring(0, 4);
    var input_month = target_date;
    var input_month = target_date.substring(4, 6);
    var input_day = target_date;
    var input_day = target_date.substring(6, 8);
    var input_hour = target_date;
    var input_hour = target_date.substring(8, 10);
    var input_minute = target_date;
    var input_minute = target_date.substring(10, 12);

        //make target date with new year, month and day as string in a date 
format required for further js processing

    var input_year = input_year.toString();
    var input_month = input_month.toString();
    var input_day = input_day.toString();

    var input_year_js = input_year + "/";
    var input_month_js = input_month + "/";

    var converted_input_date = 
input_year_js.concat(input_month_js.concat(input_day));

    //include time (hours and minutes) and add that in milliseconds to the 
input date

    var end_date = new Date(converted_input_date).getTime();
    var end_date_timezone_offset = -(new 
Date(converted_input_date)).getTimezoneOffset()*60000;
    var end_date = end_date + end_date_timezone_offset;
    var end_date = end_date + ((input_hour * 3600000) + (input_minute * 60000));

    // find the amount of seconds between now and the target date

    var current_date = new Date().getTime();
    var current_date_timezone_offset = -(new Date()).getTimezoneOffset()*60000;
    var current_date = current_date + current_date_timezone_offset;

    var seconds_diff = (current_date - end_date) / 1000;

    // calculate the difference in days based on 86,400 seconds in a day and 
adjust the level of precision based on the time difference in days that is 
returned

    if (seconds_diff > 0) {

        if (seconds_diff < 60) {
        var DaysDiff = (seconds_diff / 86400);
        var DaysDiff = (Math.floor(DaysDiff * 1000000) / 1000000);
        } else if (seconds_diff < 3600) {
        var DaysDiff = (seconds_diff / 86400);
        var DaysDiff = (Math.floor(DaysDiff * 10000) / 10000);
        } else if (seconds_diff < 86400) {
        var DaysDiff = (seconds_diff / 86400);
        var DaysDiff = (Math.floor(DaysDiff * 100) / 100);
        } else {
        var DaysDiff = (seconds_diff / 86400);
        var DaysDiff = (Math.floor(DaysDiff * 10) / 10);
        }

    } else {

        if (seconds_diff < -86400) {
        var DaysDiff = (seconds_diff / 86400);
        var DaysDiff = (Math.floor(DaysDiff * 10) / 10);
        } else if (seconds_diff < -3600) {
        var DaysDiff = (seconds_diff / 86400);
        var DaysDiff = (Math.floor(DaysDiff * 100) / 100);
        } else if (seconds_diff < -60) {
        var DaysDiff = (seconds_diff / 86400);
        var DaysDiff = (Math.floor(DaysDiff * 10000) / 10000);
        } else {
        var DaysDiff = (seconds_diff / 86400);
        var DaysDiff = (Math.floor(DaysDiff * 1000000) / 1000000);
        }

    }

return DaysDiff;
};

})();




On Thursday, 26 October 2017 19:44:51 UTC+1, Thomas Elmiger wrote:
>
> Hi Hubert
>
> Would you mind sharing your DaysDiff solution? 
>
> (I wrote my own, but it's not as solid as rpn.)
>
> Cheers,
> Thomas 
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to tiddlywiki+unsubscr...@googlegroups.com.
To post to this group, send email to tiddlywiki@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tiddlywiki/2a59d2f6-3d0b-40c8-a2be-11d34fde9bfd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to