I convert date into UTC format but day(number) is showing day before
moment('2020-3-27').utc().format('YYYY-MM-DDTHH:mm');
result = 2020-03-26T18:30
The usage pattern seems incorrect.
You can try this:
var res = moment.utc("2020-3-27", "YYYY-M-DD").format("YYYY-MM-DDTHH:mm");
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
Related
I am using moment-timezone and I am trying to format the date
like this:
moment('dic. 23 2020 11:00').format('YYYY-MM-DD HH:mm');
I am getting that it's not valid?
any idea why?
Try specifying the format parameter, after specifying the locale info. Have a look at this
const moment = require("moment");
require("moment/min/locales.min");
moment.locale("es");
const result = moment("dic. 23 2020 11:00", "MMM[.] DD YYYY hh:mm").format("YYYYMM-DD HH:mm");
console.log(result);
I have two strings that I'd like to compare as dates using moment's 'isBefore'. I tried this:
//doc.id = '18-08-2020'
//tomorrowDate = '21-07-2020'
const convertedItem = moment(doc.id, 'DD-MM-YYYY')
if (moment(tomorrowDate).isBefore(convertedItem)) //should yield true
...
You need to specify the format of both tomorrowDate and doc.id as 'dd-mm-yyyy'
moment(tomorrowDate, 'dd-mm-yyyy').isBefore(moment(doc.id, 'dd-mm-yyyy'))
When converting time to UTC its showing one hour less than expected
I am updating a variable of dot net via moment to convert the time & show local system time to user. But post conversion i am getting one hour less. Tried utcOffset but getting error utcOffset is not a function. any suggestion
Where formData.SubmittedDate = "6/7/2019 5:44:59 AM"
$('[data-utcdate]').each(function () {
var d = moment($(this).attr('data-utcdate'));
//var isDST = d.utc().local().isDST();
//var d = moment(d).utcOffset(d);
d = d.utc();
$(this).html(d.format('MMM D, YYYY h:mm A'));
})
Getting :Jun 7, 2019 12:14 AM
Expected : Jun 7, 2019 11:44 AM
From the docs:
Get the UTC offset in minutes.
So you could use a manipulation method like add with it:
$('[data-utcdate]').each(function () {
var d = moment($(this).attr('data-utcdate'));
var offset = d.utcOffset() // will return the offset in minutes
var time = d.add(offset, "m");
$(this).html(time.format('MMM D, YYYY h:mm A'));
})
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>
The ISO week numbers are not right when trying to display them on the x-axis using tickformat.
var parseDate = d3.time.format("%Y%W").parse;
var x = d3.time.scale().range([0, width]);
x.domain([parseDate("201552"), parseDate("201602")]);
var xAxis = d3.svg.axis().scale(x)
.orient("bottom")
.ticks(d3.time.monday,1)
.tickFormat(d3.time.format("%W"))
This works fine with most of the scenario except with years that have 53 weeks.
Example: from week 201552 to week 201602. In this scenario it always skips week 53 so the the tick I get are [52, 01, 02]. although the expected output is [52,53,01,02]. Here is my code
I also tried parsing the date string using momentjs since I face this problem skip week in d3.time.format
This is the modified code using momentjs to parse the date:
var parseDate = function(d){
return moment(d, "YYYYWW").toDate();
}
x.domain([parseDate("201552"), parseDate("201602")]);
var MultiFormat = d3.time.format.multi([
["%W", function(d) {
return moment(d).isoWeek();
}],
]);
var xAxis_weeks = d3.svg.axis().scale(x)
.orient("bottom")
.ticks(d3.time.monday,1)
.tickFormat( function(d) {return MultiFormat(d);})
Now the output is [51,52,01,02] which also should have been [52,53,01,02]