Convert string in javascript using moment - javascript

i try convert string to moment and check is same.
protected showEvent(event: IEvent, hour: Moment): boolean {
let formatDate: Moment = moment(event.futureDate);
console.log('--> formatDate', formatDate);
console.log('--> hour', hour);
return formatDate.isSame(hour, "hour"); // return true is same
}
this console result
--> formatDate: Moment {_isAMomentObject: true, _i: "07-24-2017 07:00:00.000", _isUTC: false, _pf: Object, _locale: Locale…}_d: Mon Jul 24 2017 07:00:00 GMT+0200 (CEST)_i: "07-24-2017 07:00:00.000"}
--> hour: Moment {_isAMomentObject: true, _isUTC: false, _pf: Object, _locale: Locale, _d: Tue Jul 25 2017 07:00:00 GMT+0200 (CEST)…}
How can I convert the date to see if the time is the same?
edit: the whole looks like this
protected showEvent(event: IEvent, hour: Moment): boolean {
return moment(event.futureDate).hour() == hour.hours()
}

Try comparing the hour value alone.
return formatDate.hours() == hour.hours()
If you are looking to compare the entire date you can compare the other values e.g. minutes()

Related

check two date with query mongodb in node js

i want to use this query but not working can you help me
dbm.collection("tasks").remove({ updatedAt: { $lt: anotherDate } })
in database => updatedAt: "Mon Jul 11 16:42:33 GMT+04:30 2022"
anotherDate:2022-07-11T13:34:26.838Z

Cookies.set doesn't set the value in cookie (js-cookie)

I'm trying to set the some data inside of cookie using js-cookie.
but somehow it's not setting in the cookie. following is my code:
const setCookie = (name: string, value: string, expires: Date) => {
Cookies.set(name, value, {expires})
}
and this is the result when I console.log this line :
UserId=1; path=/; expires=Thu, 09 Apr 2020 15:26:37 GMT
I thought that path=/ is the problem, so I've tried to set the path as well, but didn't work.
Does anyone have idea why is not setting into the cookie?
Updated:
data passed to name, value, expires are :
UserId, 1, Thu Apr 09 2020 11:26:37 GMT-0400 (Eastern Daylight Time)
Expires parameter should be a number which indicate how much days until your cookie will expire
just update your function as follow
const setCookie = (name: string, value: string, expires: number) => {
Cookies.set(name, value, {expires})
}

DateTimePicker Value Format

I have a DateTimePicker with custom format 'LT'. When I get the value, I get 'YYYY-MM-DD' format instead of ISO-8601.
$('#init').datetimepicker({
format: 'LT',
...
});
var init = $('#init').data("DateTimePicker").viewDate().format();
console.log(init); // Shows 'YYYY-MM-DD' and I want ISO-8601
If I don't format the init variable, I get a moment object with these values:
init {
_ambigTime: true
_ambigZone: true
_d: Wed Mar 20 2019 10:15:00 GMT+0100 (CET)
_f: "YYYY-MM-DD"
_fullCalendar: true
_i: "2019-03-20"
_isAMomentObject: true
_isUTC: true
_isValid: true
... }
I don't know why it has 'YYYY-MM-DD' format declared.
Thanks in advance!
-- EDIT:
I'm using Eonasdan/Bootstrap-DateTimePicker
I tried to cast the init variable with .toISOString() and I still get YYYY-MM-DD format.
It fixed parsing to UTC:
$('#init').data("DateTimePicker").viewDate().utc().format()

Moment js format date with timezone

Hi I am using Ember+moment.js to format date in my ember helpers.
I am getting the following date from the service
Tue Aug 23 2016 09:43:53 GMT+0200 (W. Europe Daylight Time)
In my ember helper class i am able to format date using following code :
var formattedDate = moment(date).format('DD/MM/YYYY h:mm a');
I am getting the following output :
23/08/2016 9:43 am
Expected Output : 23/08/2016 9:43 am GMT
How can i specify the timezone flag in the format function?
Any help should be appreciated.
Install ember-moment - ember install ember-moment
Install ember-cli-moment-shim - ember install ember-cli-moment-shim
stop and start ember server
To enable moment timezone, need to include moment:{ includeTimezone: 'all' } in config\environment.js
/* jshint node: true */
module.exports = function(environment) {
var ENV = {
modulePrefix: 'App-Name',
environment: environment,
baseURL: '/',
locationType: 'auto',
moment: {
// Options:
// 'all' - all years, all timezones
// '2010-2020' - 2010-2020, all timezones
// 'none' - no data, just timezone API
includeTimezone: 'all'
},
EmberENV: {
FEATURES: {
// Here you can enable experimental features on an ember canary build
// e.g. 'with-controller': true
}
},
APP: {
// Here you can pass flags/options to your application instance
// when it is created
}
};
return ENV;
};
and then you can start use tz function and to get abbreviated time zone name you can include z flag in the format.
moment().tz('Asia/Calcutta').format('DD/MM/YYYY h:mm a z')

How to process data read from database

I am processing data read from database on the server using the following code:
module.exports = mongoose.model('Todo', {
text : {type : String, default: ''},
created_at : Date
});
var processTodos = function ( todos ){
for (var i = todos.length - 1; i >= 0; i--) {
// Following update is not happening
todos[i].created_at = "Custom date";
};
console.dir(todos);
return todos;
};
I am not able to figure out how to update this. Is there a syntax issue that is causing this.
I am using MEAN stack for my application.
// Following update is not happening
todos[i].created_at = "Custom date";
What am i missing here.
Here is the console log for "console.dir(todos);":
{ _id: 5489dda3f23f159400475dba,
created_at: Thu Dec 11 2014 23:38:35 GMT+0530 (India Standard Time),
__v: 0,
text: 'Testing sorting at server side' }
{ _id: 5489ddacf23f159400475dbb,
created_at: Thu Dec 11 2014 23:38:44 GMT+0530 (India Standard Time),
__v: 0,
text: 'It works' }
{ _id: 5489f31a12fa54cc127f3e1d,
created_at: Fri Dec 12 2014 01:10:10 GMT+0530 (India Standard Time),
__v: 0,
text: 'time to add more data' }
If you'd like to save the changes you're making to your object, you need to persist the change using the .save() method like so:
var processTodos = function ( todos ){
for (var i = todos.length - 1; i >= 0; i--) {
// Following update is not happening
todos[i].created_at = "Custom date";
todos[i].save();
};
console.dir(todos);
return todos;
};

Categories