MomentJS TimeAgo function, when passed a non-empty string - momentjs

So I needed to have moment("2000/03/23", "YYYY/MM/DD").fromNow() to return "20 years old" instead of "20 years ago", so I decided to pass in a string. It turns out that passing any non-empty string (ie. moment("2000/03/23", "YYYY/MM/DD").fromNow("blah")) removes "ago" from the return string.. so then you can just append " old" to the end of the result.
let age = moment("2000/03/23", "YYYY/MM/DD").fromNow("blah") + " old";
I am assuming this is non-documented, non-reliable result? Why does it do this?
EDITED: replaced timeAgo with fromNow, which is the function I was actually using in my code, but mixed it up.

You want to update the format of your fromNow , then you might want to update relativeTime of updateLocale.
moment.updateLocale("en", {
relativeTime: {
future: "in %s",
past: "%s old",
s: "a few seconds",
ss: "%d seconds",
m: "a minute",
mm: "%d minutes",
h: "an hour",
hh: "%dh",
d: "a day",
dd: "%d days",
M: "a month",
MM: "%d months",
y: "a year",
yy: "%d years"
}
});
let age = moment("2000/03/23 8:15:00", "YYYY/MM/DD hh:mm:ss").fromNow();
console.log(age);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Related

moment duration error formatting with moment-duration-format

I have a problem with the formatting using moment-duration-format. I'm passing the format 'y[y] M[m] w[w] d[d] h[hr] m[min]' inside format.
But I'm getting hrs and mins instead of hr and min as the output.
eg : Im getting 1m 0w 3d 22hrs 30mins as the output.
But what I need as output is 1m 0w 3d 22hr 30min
Try updating the locale info to the format that you want. This might help you get started.
https://codesandbox.io/s/compassionate-bas-fg1c2?file=/src/index.js
var moment = require("moment");
require("moment-duration-format");
moment.updateLocale("sample", {
durationLabelsStandard: {
s: "singular second",
ss: "first plural seconds",
sss: "next plural seconds"
},
durationLabelsShort: {
s: "singular sec",
ss: "first plural secs",
sss: "next plural secs"
}
});
console.log(
moment
.duration(76800006, "seconds")
.format("y[y] M[m] w[w] d[d] h[hr] m[min]", {
locale: "sample"
})
);

netcdf - CDO monmean

I have a netcdf file, with a daily time step, that I wish to convert to a monthly time step.
The time is formatted as follow:
double time(time) ;
time:standard_name = "time" ;
time:long_name = "time" ;
time:bounds = "time_bnds" ;
time:units = "days since 2000-01-01" ;
time:calendar = "standard" ;
time:axis = "T" ;
When I convert to monthly time step using the command:
cdo monmean input.nc output.nc
Everything works fine except that the time output is strange:
time = "2000-01-16", "2000-02-15", "2000-03-16", "2000-04-15 12",
"2000-05-16", "2000-06-15 12", "2000-07-16", "2000-08-16",
"2000-09-15 12", "2000-10-16", "2000-11-15 12", "2000-12-16";
I wish to replace the day on the monthly value by the first day of the month and also remove those odd 12's for the time that appear. The desired output:
time = "2000-01-01", "2000-02-01", "2000-03-01", "2000-04-01",
"2000-05-01", "2000-06-01", "2000-07-01", "2000-08-01",
"2000-09-01", "2000-10-01", "2000-11-01", "2000-12-01";
Any hints is appreciate
cdo --timestat_date first monmean input.nc output.nc
works for me, I hope it's helpful! It places the timestamp at the first step of the averaging period, whereas the default is in the middle. (There is also a --timestat_date last if one want to do the opposite and put it at the last step of the window)

R Remove character followed by specific character in Street Address

I would like to remove everything after certain character but with few exceptions as follows:
In 1st string i want to remove everything after 'st' (my interpretation here is St represents street) and in 2nd string 'St' represents saint so would like to retain the address as it is.
In 3rd string i want to remove everything after 'Dr' (my interpretation here is Dr represents drive) and in 4th string 'Dr' represents doctor so would like to retain the address as it is.
Below is an sample input
str <- c("852 union St End",
"852 St johns street",
"30 Sandpiper Dr 35",
"30 Dr Botero drive")
My expected output is
c("852 union St",
"852 St johns street",
"30 Sandpiper Dr",
"30 Dr Botero drive")
Below is the sample code am using, however it is removing everything after St / Dr
Scrubdata <- mgsub(str,
c(" drive.*", " dr .*",
" street.*", " st .*"),
c(" drive", " dr",
" street", " st"), ignore.case = T)
Has anyone got an idea?
Thank you!
Here's a way which removes a word after 'St' or 'Dr' if there is only one word following it :
sub('(?<=(St|Dr)) \\w+$', '', str, perl = TRUE)
#[1] "852 union St" "852 St johns street" "30 Sandpiper Dr" "30 Dr Botero drive"
Using str_remove :
stringr::str_remove(str, '(?<=(St|Dr)) \\w+$')

Skipping an argument in an sprintf format string in R

I have a function that returns the string representation of a period given two dates, but I want the user to be able to control the format. For this I'm using sprintf and the caller passes in the format string, giving the caller flexibility to use things like:
> sprintf("from %s - %s","Mon 21","Sun 27")
[1] "from Mon 21 - Sun 27"
or
> sprintf("%s - %s inclusive","Mon 21","Sun 27")
[1] "Mon 21 - Sun 27 inclusive"
I've discovered that if you don't have enough % marks in the format string it doesn't matter, so you can do:
> sprintf("week begin %s","Mon 21","Sun 27")
[1] "week begin Mon 21"
but I can't find a way to construct a format string that drops the first argument and returns "week ending Sun 27". Note I really want a solution that only changes the format string, or to know if this is impossible...
I've tried tricks like making the first format 0-width with %0s but no joy:
> sprintf("%0s week ending %s","Mon 21","Sun 27")
[1] "Mon 21 week ending Sun 27"
Obviously I can make this work by making the caller say whether the format uses the start and/or end dates but if there was a way to do it with a sprintf format string that would be neat.
Like this one?
> sprintf("week ending %2$s","Mon 21","Sun 27")
[1] "week ending Sun 27"
Another example (to expand the subject),
> sprintf("week ending either %2$s or %4$s", "Mon 21","Sun 27","Mon 28","Sun 2")
[1] "week ending either Sun 27 or Sun 2"

Abbreviated relative time (Instagram style) using momentjs language wise dynamic?

moment().startOf('day').fromNow() //3 days ago.
How can I change the above to show 3d instead of language is English and to
"3 j" if language is french and so on dynamically change according to the language?
You can use moment.locale method as reported in the docs. Remember that you have to import all locales you need or you can use moment-with-locales.js that provides all supported locales.
EDIT:
To customize format of momentjs fromNow method you can use updateLocale as documented in the Customize -> Relative time section of the docs.
moment.updateLocale('en',{
relativeTime : {
future: "in %s",
past: "%s",
s: "seconds",
m: "a minute",
mm: "%d minutes",
h: "an hour",
hh: "%d h",
d: "a day",
dd: "%d d",
M: "a month",
MM: "%d months",
y: "a year",
yy: "%d years"
}
});
moment.updateLocale('fr',{
relativeTime : {
future : 'dans %s',
past : '%s',
s : 'quelques secondes',
m : 'une minute',
mm : '%d minutes',
h : 'une heure',
hh : '%d heures',
d : 'un jour',
dd : '%d j',
M : 'un mois',
MM : '%d mois',
y : 'un an',
yy : '%d ans'
}
});
moment.locale('fr');
moment().subtract(2, 'days').startOf('day').fromNow() // 2 j
moment.locale('en');
moment().subtract(2, 'days').startOf('day').fromNow() // 2 d
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment-with-locales.js"></script>

Resources