Moment locale not working - getting values in English - javascript

I am trying to set the language of moment in a function, like this:
moment.locale('nb');
const fraDato = moment('2018-05-01', 'YYYY-MM-DD').format('MMMM');
const tilDato = moment('2018-07-01', 'YYYY-MM-DD').format('MMMM');
const months = moment.months();
const fromIndex = months.findIndex(month => month === fraDato);
const toIndex = months.findIndex(month => month === tilDato) + 1;
const range = months.slice(fromIndex, toIndex);
console.log(range);
I get the returned values in English.
Here is the fiddle for it.
How can I make this work with local set to different language?

You have it misspelled. It should be moment.locale('nd');.
Check it here.

Related

How to convert a value too large for integers in JavaScript?

I am fairly new to this and please let me know if I am wrong in any point.
I am trying to convert WEI into Eth, without using the Web3 library.
const weiValue = 100000;
const ethValue = ethers.utils.formatEther(weiValue);<--- answer is correct at 0.0000000000001
My code requires me to change 40000000000000000 wei to eth.
and here is what I have:
const weiValue = 40000000000000000;
const tokenRateString = tokenRate.toString();
const ethValue = ethers.utils.formatEther();
I believe I need to convert the Wei into a string as it too large to be represented accurately as integers in JS
I am confused on what is the intermediate step here
To who ever comes here later.
Yes, a big number such like this : 40000000000000000 need to be a string.
What you need to do is this: 40000000000000000
const WeiToEth = (tokenRate)=> {
const weiValue = BigNumber.from("40000000000000000");
const ethValue = ethers.utils.formatEther(weiValue);
console.log(ethValue) }
It converts the number into a string
Here is the documentation : https://docs.ethers.io/v5/api/utils/bignumber/

Time Range with an hour difference

const all = ['2021-04-26T08:00:00', '2021-04-27T10:00:00',]
const range = ["2021-04-26T00:00:00.000Z",
"2021-04-26T01:00:00.000Z",
"2021-04-26T02:00:00.000Z",
"2021-04-26T03:00:00.000Z",
"2021-04-26T04:00:00.000Z",
"2021-04-26T05:00:00.000Z",
"2021-04-26T06:00:00.000Z",
"2021-04-26T07:00:00.000Z",
"2021-04-26T08:00:00.000Z",
"2021-04-27T00:00:00.000Z",
"2021-04-27T01:00:00.000Z",
"2021-04-27T02:00:00.000Z",
"2021-04-27T03:00:00.000Z",
"2021-04-27T04:00:00.000Z",
"2021-04-27T05:00:00.000Z",
"2021-04-27T06:00:00.000Z",
"2021-04-27T07:00:00.000Z",
"2021-04-27T08:00:00.000Z",
"2021-04-27T09:00:00.000Z",
"2021-04-27T10:00:00.000Z"
]
console.log(all, range)
is it possible to loop over a list of date like (all) and get all time range from midnight with moment.js like (range)
🙏🏻 any hint
Below is an example of achieving this. It simply loops until it finds the matching end timestamp and keeps pushing the ISO strings to the ranges array.
const all = ['2021-04-26T08:00:00Z', '2021-04-27T10:00:00Z'];
const end = moment(all[1]);
let current = moment(all[0]).startOf('day');
const ranges = [];
while (!current.isSame(end)) {
ranges.push(current.toISOString());
current = current.add(1, 'hour');
}
console.log(ranges);
<script src="https://cdn.jsdelivr.net/npm/moment#2.29.1/moment.js"></script>

Finding Difference Between Three Values Using Moment.js

I am using a variety of moment.js methods to find the difference in milliseconds between values originally represented as dates. This is what I'm doing:
const stopTimeLong = '2021-05-19 09:54:08';
const startTimeLong = '2021-05-19 09:54:04';
const currentTimeLong = '2021-05-19 09:54:10';
// Get values in milliseconds
const stopTime = moment(stopTimeLong).valueOf();
const startTime = moment(startTimeLong).valueOf();
const currentTime = moment(currentTimeLong).valueOf();
const duration = moment.duration(stopTime - startTime).asSeconds(); // is 4
const final = moment.duration(currentTime - (stopTime - startTime)).asSeconds();
Now, the value for duration here gives me what I expect based on the initial times: 4
However the value for final is 1621432450 where I would expect 6.
What is the issue here? What do I need to change in order to get 6 as the final result based on the initial dates?
The last line should be:
const final = moment.duration(currentTime - startTime).asSeconds();
The stop time is irrelevant, based on the data you gave and the result you asked for.

Facing issue in restricting the amount of splits

I have used the below code to split my string.
splitter.map((item1) => {
let splitter1 = item1.split("=")[0].trimLeft();
let splitter2 = item1.split("=")[1].trimRight();
});
where item1 contains string as
Labor_Agreement=0349BP
Default_Hours=5/8
Probation_Period=>=12 Months
The issue I am facing is to restrict the amount of splits. Because the above code will fail in case of third string , i.e. Probation_Period=>=12 Months
I tried giving parameter to restrict the amount of split in split method above, but that is giving syntax error.
An easy to understand solution would consist of first finding the first = character, and slicing you array twice to get the right portion :
const strings = [
'Labor_Agreement=0349BP',
'Default_Hours=5/8',
'Probation_Period=>=12 Months',
];
strings.map(item => {
const chSplit = item.indexOf('=');
const splitter1 = item.slice(0, chSplit).trim();
const splitter2 = item.slice(chSplit + 1).trim();
console.log(splitter1, splitter2);
});

Checking if date text has milliseconds and seconds in momentjs

I am trying to parse a date from a text format to see if milliseconds and seconds were included in it.
For e.g.
let text = '2016-02-02 14:30:34.234';
const timezone = 'America/Los_Angeles';
// const hasMS = utcParse('%Y-%m-%d %H:%M:%S.')(text);
// const hasSeconds = utcParse('%Y-%m-%d %H:%M:')(text);
const hasMS = !!moment.tz(text, 'YYYY-MM-DD HH:MM:ss.', timezone);
console.log('hasMS', hasMS);
const hasSeconds = !!moment.tz(text, 'YYYY-MM-DD HH:MM:', timezone);
console.log('hasSeconds', hasSeconds);
Basically I am trying to replace the commented code. utcParse() from d3.time-format would check if the date text has milliseconds and seconds in it. I tried a couple of things for momentjs library, but it doesn't seem to work.
By using Regular expressions you can check if certain parts of the string are present.
If you call matchDateTime() with the string you get an array back with all the matched groups.
let text_min = '2016-02-02 14:30';
let text_sec = '2016-02-02 14:30:34';
let text_ms = '2016-02-02 14:30:34.234';
function matchDateTime(text) { return text.match(/(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d)(?::(\d\d)(?:\.(\d\d\d))?)?/); }
function hasSeconds(text) { return !!matchDateTime(text)[6]; }
function hasMilliSeconds(text) { return !!matchDateTime(text)[7]; }
function testTimeString(text) {
console.log(text, "hasSeconds=", hasSeconds(text));
console.log(text, "hasMilliSeconds=", hasMilliSeconds(text));
}
testTimeString(text_min);
testTimeString(text_sec);
testTimeString(text_ms);
console.log(matchDateTime(text_ms));

Categories