I am trying to get the correct time using momentJS. Below is my code
let time = moment("1970-01-01T17:00:00.000Z", [ "h:mm A", ]).format("HH:mm");
console.log(time)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.28.0/moment.min.js"></script>
when I use the above, it returns me time as "19:00"
Can someone please suggest how do I get the correct time?
You do not have to pass any formatting like [ "h:mm A", ] when formatting a date or time from an ISO Date. You just need to use like this:
moment("1970-01-01T17:00:00.000Z").utc().format("HH:mm");
Also, we need to use .utc() function to make sure we are getting the correct UTC and not local time.
Live Demo:
let getTime = moment("1970-01-01T17:00:00.000Z").utc().format("HH:mm");
console.log(getTime) //17:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.28.0/moment.min.js" integrity="sha512-Q1f3TS3vSt1jQ8AwP2OuenztnLU6LwxgyyYOG1jgMW/cbEMHps/3wjvnl1P3WTrF3chJUWEoxDUEjMxDV8pujg==" crossorigin="anonymous"></script>
Related
So i tried to get the meridiems on luxon.js because i'm going to move my discord.js bot that is using momentjs to luxonjs because i like it more. But i got the problem that i just can't figure out how to get the meridiems of the timezone that i especify, could you help me out?
I've tried
Info.meridiems()
but i don't know how to use or what do i do with the Info part,
And then i don't understand the parameters that the give in their documentation as an example
Info.meridiems({ locale: 'my' })
it was kinda easy but i will not delete this post if someone needs it.
Basically what i did is to get the date in the format that i wanted
const time = DateTime.local().setZone('tz').toFormat("HHmmss");
in .setZone('tz') you should just put the time zone you want according to the luxon docs.
Then i would just use an if for it
const smartMeridiems = (am, pm) =>{
if(time > 120000){
pm = 'PM'
return pm;
} else{
am = 'AM'
return am;
}
}
And that is basically it
The meridiem for a DateTime can be accessed by the "a" format token:
DateTime.local().setZone(z).toFormat("a") //=> "PM"
The Luxon Info methods are for finding out what the meridiems are called in different human languages.
var moment = require('moment');
var c = moment().toDate();
How to output date without time in this format DD-MM-YYYY
Thanks
If you want to output the date in the format DD-MM-YYYY, simply use
var moment = require('moment);
var c = moment().format("DD-MM-YYYY");
toDate() converts the moment instance into a JS date, which it doesn't sound like you want to do.
I would advise looking at the Moment documentation for questions like this, it's very detailed and easy to understand.
This is my code in js:
var x = "2017-1-2";
var y = moment(x, "yyyy-MM-DD")
And when I watch y in console this is what it is:
As you can see, the _d has the correct day and monthe, but the wrong year. Why is this happening and how to fix it? I use that value to send it to server via ajax and the wrong year gets sent.
Use uppercase YYYY instead. It is mentioned in the docs. Also don't forget that construction using moment() will treat the date as local date and then convert it into UTC. If you want to treat the date as UTC, you can use moment.utc().
In my timezone (GMT+7) the code below will show "2017-01-01T17:00:00.000Z".
var x = "2017-1-2";
var y = moment(x, "YYYY-MM-DD")
console.log(y);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.16.0/moment.min.js"></script>
I have a date like "Thu Sep 01 2016 00:00:00 GMT+0530 (IST)" which I need to send to server as ISO-8601 utc time. I tried like :
moment(mydate).toISOString()
moment.utc(mydate).toISOString()
moment(mydate).utcOffset("+00:00").toISOString()
but I am getting the result like
2016-08-31T18:30:00.000Z
which is 1day behind my intended time. So what can I do to make moment ignore my local timezone and see it as UTC?
Edit:
The expected output is
2016-09-01T18:30:00.000Z
And no, the initial input isn't a string rather a javascript "new Date()" value.
Reason this happens:
This happens because .toISOString() returns a timestamp in UTC, even if the moment in question is in local mode. This is done to provide consistency with the specification for native JavaScript Date .toISOString()
Solution:
Use the same function and pass true value to it. This will prevent UTC Conversion.
moment(date).toISOString(true)
const date = new Date("2020-12-17T03:24:00");
const dateISOStringUTC = moment(date).toISOString();
const dateISOString = moment(date).toISOString(true);
console.log("Converted to UTC:" + dateISOStringUTC)
console.log("Actual Date value:" + dateISOString)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
I take the same problem today and find the solution.
Here is the solution: moment(date,moment.ISO_8601)
var date = new Date();
console.log("Original Date");
console.log(date);
console.log("After Moment Format");
console.log(moment(date,moment.ISO_8601));
Test Execution:
Moment Documentation: MomentJs
I'm developing a custom validator of a date input in my workflow form and I get a null after parsing a date this is what I done:
// check dates can be parsed
str_expiryDate = field.form.prop_wfbxTestWorkFlow_NfDate.value;
console.log("Non conformite"+str_expiryDate);
str_reminderDate = field.form.prop_bpm_workflowDueDate.value;
console.log("echeance"+str_reminderDate);
Alfresco.logger.warn("Expiry Date: " + str_expiryDate + " | Reminder Date: " + str_reminderDate);
d_expiryDate = Date.parse(str_expiryDate);
console.log("nfDate"+str_expiryDate);
d_reminderDate = Date.parse(str_reminderDate);
console.log("Date echéance"+d_reminderDate);
and then i get this in console:
Non conformite2013-06-21T00:00:00.000+01:00 echeance2013-06-09T00:00:00.000+01:00
nfDatenull
Date echéancenull
How I can parse these two dates and then compare it? .thanks
Use Alfresco.util.fromISO8601(date)
According to the client-api docs
Convert an ISO8601 date string into a JavaScript native Date object
You are parsing the "value" of a date, not the date itself.
The best way to compare is, imho, using the format YYYYMMDD, and than compare it as a number.
Something like this (there is sure a far more elegant way to do that, but at this time it's the only one that got me):
var indexDate=str_expiryDate.indexOf("-");
var dayDate=str_expiryDate.substring(0, 2);
var monthDate=str_expiryDate.substring(3, 5);
var yearDate=fromData.substring(6, str_expiryDate.length+1);
int dataNew=yearDate+monthDate+dayDate;
and than compare the two dates value.
Obviously check if the index value are correct, I didn't double checked them.
Hope il helps.