momentjs adding one week returns wrong res - momentjs

moment("20-52", "YY-ww").add(1, "week").format("YY-ww");
The result is 20-01 but I'd expect it to be 21-01
Any ideas? Is this a bug or am I doing it wrong?

You must use ISO week of years (WW), with lowercase ww moment get the week of the last day of the week in question.
const ue = moment("20-52", "YY-WW", true).add(1, "week").format("YY-WW");
console.log(ue);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Related

Not able to get the number of days correctly

I am using momentjs to try to get the difference in days between 2 dates but the days returned are not correct. The start date is 2021-04-05 and the end date is 2021-04-09 and it is returned 4 instead of 5.
I did a console log and this is what I get.
here is my code:
const [startDate, setStartDate] = useState(new Date())
const [endDate, setEndDate] = useState(new Date())
const ed = moment(endDate)
const sd = moment(startDate)
console.log(ed)
console.log(sd)
<h6>Total Number of Days: {ed.diff(sd, 'days')}</h6>
Where did I get it wrongly? Many thanks in advance and greatly appreciate any helps. Thanks
Here is how I got the difference and hope it will be useful to anyone. thanks
{Math.floor(( Date.parse(endDate) - Date.parse(startDate) ) / 86400000)}

Why moment.js adds 3 minutes to a date-time when its formatted?

When I'm formatting the date-time with moment.js, I got a strange behavior. It adds 3 minutes to the date-time.
Without specific formatting there is 08:00
moment("2018-03-25T06:00:00Z").format()
"2018-03-25T08:00:00+02:00"
But if I format the date-time, I got 08:03
moment("2018-03-25T06:00:00Z").format("YYYY/MM/DD HH:MM")
"2018/03/25 08:03"
Why is that?
const withoutFormat = moment("2018-03-25T06:00:00Z").format();
const withFormat = moment("2018-03-25T06:00:00Z").format("YYYY/MM/DD HH:MM");
console.log(withoutFormat);
console.log(withFormat);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>
Change HH:MM to HH:mm because MM is used for months in momentjs
const withoutFormat = moment("2018-03-25T06:00:00Z").format();
const withFormat = moment("2018-03-25T06:00:00Z").format("YYYY/MM/DD HH:mm");
console.log(withoutFormat);
console.log(withFormat);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>
Its just because MM gives month number. if you want 12 hour use h/hh , for 24 hour H/HH and minute m/mm. Here is doc
const withoutFormat = moment("2018-03-25T06:00:00Z").format();
const withFormat = moment("2018-03-25T06:00:00Z").format("YYYY/MM/DD hh:mm");
console.log(withoutFormat);
console.log(withFormat);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>

MomentJS using 'm' to add a month gives unexpected result

I'm using Moment.js and for some (probably basic) reason I'm not getting the result I'm expecting:
let date = moment("1995-01-25");
date.add(2, 'm');
console.log(date.month()); // Expected 2, outputs 0
You have to use uppercase M for adding months, lowercase m stands for minutes, see add docs:
Key | Shorthand
-------------------
months | M
minutes | m
Here a working sample:
let date = moment("1995-01-25");
date.add(2, 'm');
console.log(date.month());
console.log(date.format()); //1995-01-25T00:02:00
let date2 = moment("1995-01-25");
date2.add(2, 'M');
console.log(date2.month()); // 2
console.log(date2.format()); // 1995-03-25T00:00:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Getting the month number by month name with Moment.js

I am trying to return the month number passing the month name using MomentJS. For example if I pass "July" to moment() I would expect 7 to be returned.
After reading through the docs I tried several different ways, and this way came close...
console.log(moment().month("July"));
In the console, buried in the response I could see this...
_monthsParse: Array[7]
Could anyone please tell me how to return the month number using MomentJS correctly?
Try :
moment().month("July").format("M");
Relevant documentation: http://momentjs.com/docs/#/get-set/month/
alert(moment().month("July").format("M"));
<script src="https://momentjs.com/downloads/moment.min.js"></script>
Anybody looking to get month name from month number then you can try :
const number = 1; // 0 = Jan & 11 = Dec
moment().month(number).format("MMM"); // Feb
Use following to get full month name :
const number = 1; // 0 = January & 11 = December
moment().month(number).format("MMMM"); // February
To use simple month number try this:
const month = 2 //Feb
moment(month, 'M').format('MMMM');
##get month name in moment js with node js
moment() give today date
format("DD-MMMM-YYYY") / output 18-May-2020
format("DD-MM-YYYY") / output 18-05-2020
- sperator you can use /
```
var moment = require('moment');
m_date = moment().format("DD-MMMM-YYYY");
console.log("moment date :", m_date)
```
##output
```
moment date : 18-May-2020
```
Read Officail Docdescription here

DateTime Parse from FTP

I am using FTP and I have retrieved a list of Files. In the files command line there is a datetime field.
It reads
11-13-13 11:31AM
Can anyone tell me how I can parse this. I thought this might work.
DateTime.ParseExact(date,"MM-DD-YY HH:MMtt", System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat); But i still get an exception.
day should be dd not DD
Year should be yy not YY
since hour is 12 hour format it should be hh also MM (minutes should be mm)
DateTime.ParseExact(date,"MM-dd-yy hh:mmtt",...)
eg:-
DateTime date = DateTime.ParseExact("11-13-13 11:31AM", "MM-dd-yy hh:mmtt", System.Globalization.CultureInfo.InvariantCulture);
Response.Write(date);

Resources