I'm using moment.js for my react-native project datetime formater. I want to remove the timezone from utc date. For my timezone is GMT+8000, so for utc offset is correct, when when I add format('YYYY/MM/DD 00:00'), result subtract one day.
console.log(moment.utc());
-> Moment {_isAMomentObject: true, _isUTC: true, _pf: {…}, _locale: Locale, _d: Wed Jul 11 2018 11:08:57 GMT+0800 (Malaysia Time), …}
console.log(moment.utc().utcOffset(new Date().getTimezoneOffset()));
->Moment {_isAMomentObject: true, _isUTC: true, _pf: {…}, _locale: Locale, _d: Wed Jul 11 2018 03:08:57 GMT+0800 (Malaysia Time), …}
console.log(moment.utc().utcOffset(new Date().getTimezoneOffset()).format('YYYY/MM/DD 00:00'));
->"2018/07/10 00:00"
Any idea how ? Or maybe I doing it wrong ?
Related
Some context:
In my react-native app I'm trying to check two dates to see if there is a day difference. If there is a day difference, perform a function.
The problem is, I have to store today's date (done with var today = moment() ) as a string into AsyncStorage, and then retrieve that string next time the app is launched and turn it back into a moment object.
First object is called today and the other is lastChecked. When I run var dayDiff = today.diff(lastChecked, 'days') it returns 0.
I ran console.log on both objects and here is what it shows.
_d is the same on both objects, however the second object has a _i object that contains my desired date taken from the AsyncStorage.
How would I correctly set the lastChecked object to contain that date and compare day difference using that date?
Moment {_isAMomentObject: true, _isUTC: false, _pf: {…}, _locale: Locale, _d: Thu Oct 03 2019 00:00:00 GMT+0800 (Australian Western Standard Time), …}
_d: Thu Oct 03 2019 00:00:00 GMT+0800 (Australian Western Standard Time) {}
_isAMomentObject: true
_isUTC: false
_isValid: true
_locale: Locale {_calendar: {…}, _longDateFormat: {…}, _invalidDate: "Invalid date", _dayOfMonthOrdinalParse: /\d{1,2}(th|st|nd|rd)/, ordinal: ƒ, …}
_pf: {empty: false, unusedTokens: Array(0), unusedInput: Array(0), overflow: -2, charsLeftOver: 0, …}
__proto__: Object
index.js:176
Moment {_isAMomentObject: true, _i: "2019-10-02T16:00:00.000Z", _f: "YYYY-MM-DDTHH:mm:ss.SSSSZ", _tzm: 0, _isUTC: true, …}
_d: Thu Oct 03 2019 00:00:00 GMT+0800 (Australian Western Standard Time) {}
_f: "YYYY-MM-DDTHH:mm:ss.SSSSZ"
_i: "2019-10-02T16:00:00.000Z"
_isAMomentObject: true
_isUTC: true
_isValid: true
_locale: Locale {_calendar: {…}, _longDateFormat: {…}, _invalidDate: "Invalid date", _dayOfMonthOrdinalParse: /\d{1,2}(th|st|nd|rd)/, ordinal: ƒ, …}
_offset: 0
_pf: {empty: false, unusedTokens: Array(0), unusedInput: Array(0), overflow: -1, charsLeftOver: 0, …}
_tzm: 0
__proto__: Object
edit: Fixed code formatting
I've resolved the issue.
The problem was that the LastChecked parameter was never set to .startOf('day') and took in the actual time (in this case being 16:00).
Calculating the time difference in days would return 0 as the default function returns only integers. Passing a bool like this: var dayDiff = today.diff(LastChecked, 'days', true) would return a float value.
In my case it returned 0.66
Additionally, if you want to see what date it actually compares then call console.log(today.toString())
The other problem I found was that because I initialised the LastChecked moment object as let LastCheck = moment() it took on current time. So when initialising, set it to: let LastCheck = moment().startOf('day')
I am using daterangepicker to select the start and the end date.
This is my JsFiddle example
The date is working and I can select the start and the end date.
<input type="text" class="date" ng-model="selectDate" />
But how can I pass the selectDate model to the filters so that only those events will be selected where selectDate will match the eventStartDateTime
$scope.data=[{'eventStartDateTime': 'Tue, 02 April 2019, 12:30 PM','eventName': 'ANew Event','itemCreatedDateTime': '3/04/2019 5:17:10 AM',},{'eventStartDateTime': 'Tue, 02 April 2019, 02:43 PM','eventName': 'AFeatured Event 3','itemCreatedDateTime': '2/04/2019 1:54:10 AM',},{'eventStartDateTime': 'Tue, 02 April 2019, 12:30 PM','eventName': 'Event 9','itemCreatedDateTime': '2/04/2019 1:29:56 AM',},{'eventStartDateTime': 'Thu, 28 March 2019, 04:30 AM','eventName': 'Featured Event 2','itemCreatedDateTime': '28/03/2019 4:59:13 AM',},{'eventStartDateTime': 'Tue, 02 April 2019, 12:55 PM','eventName': 'Featured Event 4','itemCreatedDateTime': '28/03/2019 4:58:54 AM',},{'eventStartDateTime': 'Thu, 28 March 2019, 04:30 AM','eventName': 'Avent 5','itemCreatedDateTime': '28/03/2019 1:29:06 AM',},{'eventStartDateTime': 'Thu, 28 March 2019, 05:30 AM','eventName': 'Event 4','itemCreatedDateTime': '28/03/2019 1:29:00 AM',},{'eventStartDateTime': 'Fri, 29 March 2019, 04:00 AM','eventName': 'Event 3','itemCreatedDateTime': '28/03/2019 1:28:54 AM',},{'eventStartDateTime': 'Thu, 21 March 2019, 04:30 AM','eventName': 'Event 2','itemCreatedDateTime': '28/03/2019 1:28:41 AM',},{'eventStartDateTime': 'Thu, 28 March 2019, 04:00 AM','eventName': 'Event 1','itemCreatedDateTime': '28/03/2019 1:28:36 AM',}];
Any help or suggestion would be appreciated.
Thanks in advance
You can use the Angular.js directive for daterangepicker
Install it then add daterangepicker to your angular.module and initialize your variables:
var app = angular.module("myApp", ["daterangepicker"]);
app.controller("myCtrl", function($scope, $window) {
...
$scope.showFreeEvent = false;
$scope.selectDate = { date: { startDate: null, endDate: null } };
...
Then in your HTML add attribute date-range-picker to any input and bind it to model:
<input
date-range-picker
class="form-control date-picker"
type="text"
ng-model="selectDate.date"
/>
And to filter your events you can use moment().isBefore() and moment().isAfter():
if (!$scope.showFreeEvent) {
return true;
}
if (
$scope.selectDate.date.startDate.isAfter(el.eventStartDateTime) ||
$scope.selectDate.date.endDate.isBefore(el.eventStartDateTime)
) {
return false;
}
Demo: https://codesandbox.io/s/l29yqywx9m
add ng-onchange="filterfunction(dateModelInput)"
this will detect a change in your html,call your filter function. this will update your DOM.
make sure you include the filter into the html that needs the filtering.
<div>{{ctrl.data | filterResult }}</div>
I have an object
{
YHOO: [
{
date: Fri Apr 12 1996 00:00:00 GMT-0400 (EDT),
open: 25.25,
high: 43,
low: 24.5,
close: 33,
volume: 408720000,
adjClose: 1.38,
symbol: 'YHOO'
},
...
{
date: Thu Nov 14 2013 00:00:00 GMT-0500 (EST),
open: 35.07,
high: 35.89,
low: 34.76,
close: 35.69,
volume: 21368600,
adjClose: 35.69,
symbol: 'YHOO'
}
],
GOOGL: [
{
date: Thu Aug 19 2004 00:00:00 GMT-0400 (EDT),
open: 100,
high: 104.06,
low: 95.96,
close: 100.34,
volume: 22351900,
adjClose: 100.34,
symbol: 'GOOGL'
},
...
{
date: Thu Nov 14 2013 00:00:00 GMT-0500 (EST),
open: 1033.92,
high: 1039.75,
low: 1030.35,
close: 1035.23,
volume: 1166700,
adjClose: 1035.23,
symbol: 'GOOGL'
}
],
...
}
How do I alter this object so it becomes an array with
[
{
date: Fri Apr 12 1996 00:00:00 GMT-0400 (EDT),
YHOO: 33,
GOOG: 100.34
},
...
]
I know it can become a problem that they potentially not have the same dates, so I guess I should just say that one of the companies have the right dates, and then merge the other companies to these dates and omit those dates that are not in the already selected company.
I guess I should do something like
var data = [];
obj.YHOO.forEach((quote) => {
data.push({
date: quote.date,
YHOO: quote.price,
GOOG: ?
});
});
and instead of the question mark, I can loop through all the objects in array.GOOG and check if the date matches quote.date.
The big problem is that this seems quite computational heavy and the names of the companies can vary, so I cannot guarantee that YHOO and GOOG are present.
I am trying to parse a JSON object that comes in from mongodb but am unable to use it for some reason after parsing.
Here is the piece of javascript function that I am using
-function check(category) {
-var temp = JSON.stringify(category);
-return JSON.parse(temp);
-}
.row.wow.fadeInRight.animated(data-wow-offset='30', data-wow-duration='1.5s', data-wow-delay='0.15s')
.col-md-12
if(articles)
#client-feedbacks.owl-carousel.owl-theme
each article in articles
-var category = article.categories
.feedback-box
.client-image.hidden-xs
img(src=article._.image.fit(250,250), alt='article image')
// MESSAGE OF THE CLIENT
h6.article-title
a(href='/blog/post/' + article.slug, itemprop='url')!= article.title
br
.message!= article.content.brief
// CLIENT INFORMATION
.client
.quote.red-text
i.icon-fontawesome-webfont-294
.client-info
a.client-namea(href='/blog/post/' + article.slug, itemprop='url')!= prettyDate(article.publishedDate)
-var categories = check(category)
.client-company(itemtype="http://schema.org/BlogPosting") #{category[0].name}
Here is what the object looks like when i use json .stringify
{ updatedBy: 5498631c5cbfa2d03f4570e6,
updatedAt: Mon Feb 09 2015 00:30:48 GMT+0530 (IST),
createdBy: 5498631c5cbfa2d03f4570e6,
createdAt: Mon Feb 09 2015 00:30:48 GMT+0530 (IST),
key: 'mongoose',
name: 'Mongoose',
_id: 54d7b2601d88fc4f69d0e081,
__v: 0 },{ updatedBy: 5498631c5cbfa2d03f4570e6,
updatedAt: Mon Feb 09 2015 00:31:44 GMT+0530 (IST),
createdBy: 5498631c5cbfa2d03f4570e6,
createdAt: Mon Feb 09 2015 00:31:44 GMT+0530 (IST),
key: 'green-berrets',
name: 'Green Berrets',
_id: 54d7b298fe85a1c4698b2ec3,
__v: 0 }
I am confused on how to parse it as most of the things i tried did not work
This
moment(1420066800000);
returns a moment object with an invalid date, why?
EDIT:
moment(1420066800000).isValid()
returns true! But: the moments' property
_d.__proto__
is an "Invalid Date" object. That's what confused my, since I thought this was causing wrong behaviour in my application.
What is wrong with Wed Dec 31 2014 23:00:00 GMT+0000 (UTC) ?
> d = moment(1420066800000);
{ _isAMomentObject: true,
_i: 1420066800000,
_f: undefined,
_l: undefined,
_strict: undefined,
_isUTC: false,
_pf:
{ empty: false,
unusedTokens: [],
unusedInput: [],
overflow: -2,
charsLeftOver: 0,
nullInput: false,
invalidMonth: null,
invalidFormat: false,
userInvalidated: false,
iso: false },
_d: Wed Dec 31 2014 23:00:00 GMT+0000 (UTC) }