Sequelize behavior on DateTime - javascript

The datetime in table is 2018-01-03 15:46:59.807438 in postgres. But the result from sequelize shows time as 2018-01-03T20:46:59.807Z. I don't see any manual conversion done. Could you please help understand reason why. Somewhere i feel the time is converted to/from UTC

In Postgres datetime shown in local timezone (to see current timezone: show timezone;). Response from sequelize shown in zero timezones (Z at the end of the date, ref: https://www.timeanddate.com/time/zones/z). So, it's the same date, but the first one is the date in your local timezone, the second one - UTC.

Related

Dates manipulation with moment.js

I receive the following date from the server:
"2018-11-21 07:00:00 UTC"
Then, I convert it using userTimzone variable (since I wish the editor would use the user timezone):
dateOfAction: moment(dateOfLoss, 'YYYY-MM-DD HH:mm Z').tz(userTimzone).unix(), So dateOfAction is 1542776400. That is: Wednesday, November 21, 2018 5:00:00 AM - and so far so good. That is the dateOfAction in UTC with after right offset
I'm using react-datetime as the calendar the edit the date, using UTC.
When performing the save action, without touching the date, the calendar output is still 1542783600, but I wish to be 1542783600 - i.e the same value as in the beginning, reverting the offset at the other direction.
How can I achieve that?
A few things:
Moment doesn't map the Z token to the string "UTC". Since you're parsing in local mode, you are actually getting a moment based on the local computer's time zone rather than UTC. Thus, change the first part of your code to:
moment.utc(dateOfLoss, 'YYYY-MM-DD HH:mm [UTC]')
The brackets are to treat UTC as a literal string, which isn't strictly required so you can omit it if you like and the result will be the same.
moment.utc(dateOfLoss, 'YYYY-MM-DD HH:mm')
There's no need to call .tz(userTimezone) if you're just going to call .unix() subsequently. Unix timestamps are always UTC based. Though it's not clear why you're asking for a Unix timestamp, as react-datetime doesn't need one.
The two values you gave in the last paragraph of your question are identical, so I'm not sure specifically what you were looking for. The time you gave is indeed 1542783600, not 1542776400.
The readme file of react-datetime describes all the options you can use. You can simply pass the moment object obtained above to the value prop. You might need to use the utc or displayTimeZone props if you want to change the behavior. You might also need to call .local() or .tz(userTimezone) on the moment object before passing it in, but I'm not certain if that is required or not for this particular component.

Convert hour to date time without adding +1

Hi im using moment js to convert this string 20:00 I tried:
var a = moment("20:00", "HH:mm")
console.log(a.format()) // 2016-09-08T20:00:00+01:00
the problem when I store in mongodb it become
2016-09-10T19:00:00.000Z
I want to store 2016-09-10T20:00:00.000Z
anyway can explain why please ?
When you say that you want to store 2016-09-10T20:00:00.000Z what you are saying is that you want to assume that your date and time is UTC.
To assume that the date you are parsing is a UTC value, use moment.utc
var a = moment.utc("20:00", "HH:mm")
console.log(a.format()) // 2016-09-08T20:00:00Z
Note that when you parse a time without a date, moment assumes the current date. This may not be the behavior that you want.
I'm also not sure if you want a UTC date (which is what you are saying), or a local date without an offset indicator. If you want a local date without an offset indicator, simply use a format without an offset:
moment.utc("20:00", "HH:mm").format('YYYY-MM-DDTHH:mm:ss.SSS')
"2016-09-08T20:00:00.000"
If you are dealing with local dates that do not have a time zone association, I recommend using moment.utc to parse, as this will ensure that the time does not get shifted to account for DST in the current time zone.
For more information about how to parse dates into the time zone or offset that you would like in moment, see my blog post on the subject.
This it how it should look:
var a = moment("20:00", "HH:mm")
console.log(a.utcOffset('+0000').format())
<script src="http://momentjs.com/downloads/moment.min.js"></script>
Doe, the problem is that you are using timezones when you create the date.
MomentJS uses your current timezone automatically.
Mongo however saves the time as it would be in another timezone.
Therefore, if you want the two strings to format the same way, you need to set the timezone.

Moment Timezone : I don't understand how default timezones work

Say it's 10:00am in my local time, America/Denver. When I set the default timezone in moment timezone:
moment.tz.setDefault("America/Chicago");
Current time objects are printed out in the correct timezone, America Chicago:
moment().toString(); // returns 11:00am
However, when I parse in a date, for example:
// exampleTime equal to 9:05am, already in America/Chicago
var parsedTime = moment(exampleTime, "HH:mm")
It seems to take the date, parse it in the local timezone ("America/Denver") and then "convert" it to America/Chicago. So, when I:
parsedTime.toString(); //prints ~10:05am
It prints out a time that is an hour ahead of what it should be.
Do I have to specify the timezone of every time I parse in? Why wouldn't it parse in the default timezone?
You're using it correctly, you're just hitting on a bug. It's already been logged in this issue, and there's a pending fix here.

Keeping the time fields but changing the timezone with moment.js

I have a moment object to which I need to apply a timezone, but without altering the values of the hour or minute field. For example, if my moment contains the date '2013-10-10T15:00:00+00:00' I want to be able to change the zone to 'America/Los_Angeles' so that when I print the moment I obtain '2013-10-10T15:00:00-07:00'
(for those of you familiar with Joda I'm after the withZoneRetainFields() functionality)
Here is a jsfiddle with the basic setup, showing the problem. How can I alter the last item so that it gives the desired output?
I recommend looking into the moment-timezone.js library for managing timezones. It offers an interesting .zone() function for manipulating timezones on a moment date object.
I've included a link to the relevent issue on moment.js's GitHub issues:
Switching timezone of moment without changing the values
Note, though I don't recommend it, you can also manually adjust a date for the desired timezone. In the below example, I'm creating a date in the client's timezone, changing the timezone on the date object to UTC, then fixing the value of the date such that the hours and minutes retain the same value.
// Takes a moment.js date object, and converts
// it to UTC timezone, while maintaining the
// selected hour/mins of the input date.
function convertDateToUTC (date){
// Current timezone's offset. Minutes offset from UTC
var offset = date.utcOffset();
// Convert selected date to UTC
date = date.utc();
// Adjust the time to.
date.add(offset, "minutes");
return date;
};

Query by date i mongo from node.js

I want to query mongo collection by date. Example:
var startDate = new Date(dateNow.getUTCFullYear(),dateNow.getUTCMonth(),dateNow.getUTCDate(),dateNow.getUTCHours(),0);
var endDate = new Date(dateNow.getUTCFullYear(),dateNow.getUTCMonth(),dateNow.getUTCDate(),dateNow.getUTCHours()+1,0);
query.timeRegistered = { '$gte' : startDate, '$lt' : endDate };
... make mongo query ...
But it doesn't work. I assume that is because mongo saves date object in ISODate format. This query works from shell because there mongo converts Date to ISODate but from javascript (node.js) it doesn't work. I've tried all possible solutions but neither of them helped me.
Please, if anyone has any solution I would be very gratefull....
Please consider that by default the date operation in Mongo are in UTC, e.g. I entered the record in "t1" collection at June 22nd 2015 at 7:10 PM IST. however, in the shell you can see its value as 'ISODate("2015-06-22T13:40:08.545Z")' which is 5:30 hours behind. from your javascript code (which is run on the browser and hence take the browser timezone) try to create the start and end date variables as per the UTC timezone and then query the records. Let us know if it does't work, to keep it simple, give a very large date range and see if its working or not. as I am not a JS expert, I assume you may need to adjust your dates to make it in UTC.
however, tried with the Mongo client as below and it worked so I assume from JS also it should work as long you pass the date in right timezone.
db.t1.find({dt:{$gt:ISODate("2015-06-22T13:40:00.000Z"),$lt:ISODate("2015-06-22T13:41:00.000Z")} })

Categories

Resources