I have a really simple piece of code for moment.js (see below), it should resolve to true but instead resolves to false.
I get the same unexpected behaviour with isBefore() or isAfter(), which leads me to believe there is something wrong with how I'm defining the dates.
var format = 'YYYY-MM-DDTHH:mm:ss.SSSSZ';
var testTime = moment('Thu Jun 27 2019 05:33:19 GMT+0000', format);
var startPeriod = moment('Thu Jun 27 2019 04:00:19 GMT+0000', format);
var endPeriod = moment('Thu Jun 27 2019 10:00:19 GMT+0000', format);
console.log(
testTime.isBetween(startPeriod, endPeriod)
);
What am I missing here?
The second parameter to moment() takes in the format that the first parameter is currently in. This looks incorrect. You can verify this by logging the times individually and seeing that they're incorrect (probably 1970)
See https://momentjs.com/docs/#/parsing/creation-data/
Infact, you should be able to drop this second parameter completely as the input date is in a standard format already (an ISO string).
var testTime = moment('Thu Jun 27 2019 05:33:19 GMT+0000');
var startPeriod = moment('Thu Jun 27 2019 04:00:19 GMT+0000');
var endPeriod = moment('Thu Jun 27 2019 10:00:19 GMT+0000');
console.log(
testTime.isBetween(startPeriod, endPeriod)
);
Related
I have a date coming from database which is 2022-10-31 11:33:07.861Z, and in my js file I have this date is saved in a variable :
function test() {
let now = new Date().toLocaleString('en-GB', { timeZone: 'Europe/London'}); // I need now to be Europe/london time
let dateFromDb = db_date; // 2022-10-31 11:33:07.861Z
// I have a function which takes ^ above date and returns this : Mon Oct 31 2022 11:33:07 GMT+0000 (Greenwich Mean Time)
let formatedDate= this.toDate(dateFomDb); // returns : Mon Oct 31 2022 11:33:07 GMT+0000 (Greenwich Mean Time)
}
When I console.log(now, typeOf now) I get this: 02/11/2022, 23:24:52 string
and console for formatedDate returns this : Mon Oct 31 2022 11:33:07 GMT+0000 Object
I need to compare both dates :
return newDate <= now; // expect it to true or false;
Can anyone please suggest me how can I do this ?
I tried to cast both dates toLocalString but its string from I need object.
I have the following code to increment the hours in a date:
let timerExpireDate = new Date(countdownStartDate);
console.log(`original date is ${timerExpireDate}`);
console.log(`add on ${countdownHours} hours`);
timerExpireDate.setHours(timerExpireDate.getHours() + countdownHours);
console.log(`New date is ${timerExpireDate}`);
However it also seems to be incrementing the days by 6, here is the console log:
original date is Sun Jul 19 2020 16:36:39 GMT+0800 (Taipei Standard Time)
add on 2 hours
New date is Sat Jul 25 2020 18:36:39 GMT+0800 (Taipei Standard Time)
What am I doing wrong here?
It is likely that countdownHours is of type string instead of number, so timerExpireDate.getHours() + countdownHours is '162' (6 days later) instead of 18.
The fix is to cast countdownHours to number first, like countdownHours = +countdownHours.
After isolating the problem in my current project .... I think something is not working currently with the Date object in javascript.
But I don't want to be arrogant because maybe I am doing something wrong, is it a bug? or am I doning something wrong?
I want to convert a date to diffrent time zone in javascript, I made this function:
function ConvertToTimeZone(date, timezone) {
let convertedTimeString = date.toLocaleString("en-US", {hour12: false, timeZone: timezone})
return new Date(convertedTimeString)
}
Then I am using it like this:
let testDate = new Date("Thu May 21 2020 17:04:05 GMT-0700 (Pacific Daylight Time)");
let convertedDate = ConvertToTimeZone(testDate, "Europe/London");
Or like this:
let testDate = new Date("Thu May 21 2020 18:04:05 GMT-0700 (Pacific Daylight Time)");
let convertedDate = ConvertToTimeZone(testDate, "UTC");
But something wrong is happening when I use this date:
let testDate = new Date("Thu May 21 2020 17:04:05 GMT-0700 (Pacific Daylight Time)");
let convertedDate = ConvertToTimeZone(testDate, "UTC");
with that last exmaple convertedDate end up with Invalid Date {}
And when I look closely in my ConvertToTimeZone the result of date.toLocaleString(...) is 5/22/2020, 24:04:05 which looks strange because the hours of that time cannot be 24 (it should be 0)
I have a requirement to send the current system date to microservices on search. The time should include milliseconds information as well. For now I was sending new Date() for the same and it looked like:
Thu Aug 31 2017 15:06:37 GMT+0530 (India Standard Time)
However I need the milliseconds information as well so the time should look like:
Thu Aug 31 2017 15:06:37.228 GMT+0530 (India Standard Time)
Here 228 is the millisecond at that moment that I can extract using getMilliseconds() method of date. The question is how can I add this in the date so that it works for all locations wherever the application is accessed?
If you don't mind having the result as a string, this will show the output you are looking for:
// ES5
var fmtDateMsES5 = function(date) {
var splitDate = date.toString().split(' ');
splitDate[4] = splitDate[4] + '.' + date.getMilliseconds();
return splitDate.join(' ');
}
// log output (ES5)
console.log('ES5 output\n', fmtDateMsES5(new Date()));
// ES6
const fmtDateMsES6 = date => {
const splitDate = date.toString().split(' ');
splitDate[4] = `${splitDate[4]}.${date.getMilliseconds()}`;
return splitDate.join(' ');
};
// log output (ES6)
console.log('ES6 output\n', fmtDateMsES6(new Date()));
// ES5 and ES6 functions logged simultaneously
console.log(
`\nES5 and ES6 functions logged simultaneously`,
`\n${'-'.repeat(55)}`,
`\nES5 output ${fmtDateMsES5(new Date())}`,
`\nES6 output ${fmtDateMsES6(new Date())}`
);
Initially I saw the format method on the Date object but this is not built-in and requires a library.
If you must use a time library I would recommend the excellent moment.js and use the "SSS" syntax to get the milliseconds, for example:
var now = moment().format('MMM DD h:mm.SSS A');
//Sep 12 8:21.167 AM
http://jsfiddle.net/kLL2eobh/
I have the following timestamp: 2016-03-29T14:14:43.000Z. Is there any easy way to use JavaScript to make it look something like the following: Mar 29, 2016 2:14p? I tried using Date.parse() but it didn't seem to do anything.
{{yourValue| date:"MMM d, yyyy h:ma"}}
var ts = "2016-03-29T14:14:43.000Z";
var date = new Date(ts);
console.log(date); // Displays Tue Mar 29 2016 16:14:43 GMT+0200 (Romance Summer Time)
Is that what you need ?
var ds = date.toUTCString();
console.log(ds.substr(0,24)); // Displays Tue Mar 29 2016 16:14:43