I have a very strange behavior while extracting hours and minutes from a Date object. my initial object date is displaying 2017-01-16T19:00:00. here is the sample code:
var dt = new Date(date);
console.log("intial start date: "+date.format());
console.log("start date: "+dt);
but when I create a new Date object (dt) to extract hour and minute, I can see 1 more hour from the initial value....timezone seems to be correct....
here are the logs:
intial start date: 2017-01-16T19:00:00
start date: Mon Jan 16 2017 20:00:00 GMT+0100 (Romance Standard Time)
start date hours: 20 Minutes: 0
In fact I just want to extract hours and minutes from date, how can I do that with the correct hour and minutes ?
Apparently you are using fullCalendar, which uses Moment to handle dates. So when you call .format(), you call a method defined by Moment. The documentation on that method says:
As of version 1.5.0, calling moment#format without a format will default to moment.defaultFormat. Out of the box, moment.defaultFormat is the ISO8601 format YYYY-MM-DDTHH:mm:ssZ
So, by not specifying an argument to .format(), the date/time is rendered in the GMT 00:00 time zone, which is not yours. To solve this, provide a format argument, for example like this:
date.format('DD/MM/YYYY HH:mm:ss')
Related
I am using Node.js with pg to query a Postgres database. I am trying to execute the following query:
SELECT COUNT(*) FROM table_name WHERE date_time_added::date = some_date;
some_date is the date I pass from Node.js.
I have no problems with this most of the time, as both date/time strings are in the same format. However, issues arise when Timezones are introduced. As I am located in the UK, we use British Summer Time (GMT+1) for some parts of the year.
When I cast date_time_added from it's stored type (timestamp with timezone) to date, the time defaults to midnight. The issue with this is that during BST, the timestamp is cast to one day earlier, and the time set to 23:00 instead of 00:00, as shown below:
{ date_time_added: 2020-09-28T23:00:00.000Z }
This wouldn't be a problem if JavaScript had the same behaviour, however if I try the following:
const date1 = new Date(2020, 8, 29, 10, 47, 54);
console.log(date1.toString());
date1.setHours(0);
date1.setMinutes(0);
date1.setSeconds(0);
date1.setMilliseconds(0);
console.log(date1.toString());
The following output is produced:
"Tue Sep 29 2020 10:47:54 GMT+0100 (British Summer Time)"
"Tue Sep 29 2020 00:00:00 GMT+0100 (British Summer Time)"
The time is set to midnight rather than 23:00 the day before. This is a problem for me as this means the dates don't match, and therefore the row is not returned in the database query.
Does anyone know how I can get the same behaviour for both dates (JS and Postgres)? It would be good to be able to initialise a new Date Object with the time defaulting in the same way as Postgres does when I cast to date, however I am not sure if this is possible.
Posting in case anyone else comes across this problem in the future.
This issue was caused because casting a timestamp to a date in Postgres loses the timezone, therefore it was subtracting an hour from midnight (as the time is set to midnight when casting a timezone to a date), causing it to become 23:00 one day before the date (as BST is +1 hour).
I decided to use epoch time as this made comparison easier for my needs.
I solved this by using the following query:
SELECT COUNT(*) FROM table_name WHERE extract(epoch from date_trunc('day', date_time_added)) = some_date_epoch;
This keeps the timezone and returns the epoch time (at midnight) for the date of the selected timestamp. 'some_date_epoch' is the epoch time of a JavaScript Date Object, obtained using the Date.getTime() method. This is the epoch time of the date I'd like to search for in the database.
For example, this is how I'd get the epoch time for the current date in JavaScript, to then use in the database query:
let currentDate = new Date();
currentDate.setHours(0);
currentDate.setMinutes(0);
currentDate.setSeconds(0);
currentDate.setMilliseconds(0);
let currentDateEpoch = (currentDate.getTime() / 1000); //Convert to seconds to match Postgres
currentDateEpoch would then be used to query the database.
I have a problem with the date recognition of Google Apps Script. I have a code where I need to add days to a given date. However, the code returns the wrong month (higher not lower what would be a typical GAS problem). I wrote the following code to test the behaviour:
var datumStart = new Date();
datumStart = arrayTimesheet[5][7];
var datum2 = new Date();
datum2.setDate(datumStart.getDate());
and I receive the following results for the two variables:
datumStart Date Mon Feb 17 2020 09:00:00 GMT+0100 (Central European Standard Time)
datum2 Date Tue Mar 17 2020 22:50:39 GMT+0100 (Central European Standard Time)
the Array[5][7] is the value of a Google Sheets cell with the value 2020-17-02.
This datum2 variable is just a test variable to understand the error.
Has anybody an idea what is going wrong?
Javascripts getDate() and setDate() functions get and set the Day of the month.
So, your code is behaving as expected, you are instantiating the date datum2 in March, so it is a date in March (current date/time), later, you use setDate() to set it to the 17th of March.
To add days to a date, you can instantiate a new date using values from your existing date, plus the additional days, eg here we are getting a date 5 days in the future:
var future_day = new Date(datumStart.getFullYear(),datumStart.getMonth(),datum.getDate()+5);
Note that Javascript's underlying Date logic is solid, if your number of days crosses a month or year boundary, the results will be correct. (for example, if you add 31 days, you'll get the correct date in the following month)
I need to get format like below
19 Aug 2015 04:22:36 GMT
I have following code
var dt = '2015-08-19 04:22:36';
alert(new Date(dt).toUTCString().substr(4))
It returns me
18 Aug 2015 22:52:36 GMT
where as it should return
19 Aug 2015 22:52:36 GMT
What is wrong in my code
JsFiddle
As per answers below it seems it's converting the date to UTC date time.
I have date in UTC format in database. So please if some one could suggest the desired format without using toUTCString()
Update
Tried following
var dt = '2015-08-19 04:22:36 UTC';
alert(new Date(dt).toUTCString().substr(4))
It gives me liddate in FireFox and IE, chrome it is fine
Solved changed date string to
var dt = '2015/08/19 04:22:36 UTC';
Thanks
The toUTCString() converts your date to UTC so the outcome is correct!
Change your string to this var dt = '2015-08-19 04:22:36 UTC'; (notice the UTC)
or use the Date.UTC() function:
alert(new Date(Date.UTC(2015, 09, 19, 04, 22, 36)).toUTCString().substr(4))
Notice that the month is 0 based (0 -11) so to get August
you need to increase your monty by 1
From your profile, I can see you are 5 hours and 30 minutes ahead of UTC, so the var dt = '2015-08-19 04:22:36'; returns a Date object which is 5 hours and 30 minutes ahead of UTC time i.e. your local time. When you convert this date to UTC, it subtracts 5.5 hours and returns the Date object which is 22:56 previous night. The time part 04:22:36 is actually 4:22 a.m. or 4:22 a.m. at morning.
I have a js timestamp of Tue Sep 30 2014 12:02:50 GMT-0400 (EDT)
with .getTime() I got 1412092970.768
for most cases, its a today's specific time stamp. I wonder, if I could always ONLY pick out the day month and year and hour, min, day will be always stay with 0.
So for our situation, it should become Tue Sep 30 2014 00:00:00 GMT-0400 (EDT).
I wonder what kind of conversion should I be doing? Because seem convert to unix timestamp with getTime() will result in unknown way of calculation... and I can not really find a way to set time like I would do in PHP.
Any fix for this situation?
Thanks
You can create a date object and then zero-out any components you don't need, or create one with the components you specified, e.g.
foo = new Date();
foo.setHour(0);
foo.setMinute(0);
or something more like
foo = new Date(); // "now"
bar = new Date(foo.getYear(), foo.getMonth(), foo.getDate(), 0 , 0, 0, 0);
// create new date with just year/month/day value, and time zeroed-out.
The constructor's args are detailed here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
An other option is to send epoch to PHP:
JS:
long epoch = System.currentTimeMillis()/1000;
PHP:
$dt = new DateTime("#$epoch");
$dt->format('Y'); //year
I've created a date in JS like so:
var myDate = new Date('2013-01-01 00:00:00');
I assume JS reads this in as UTC time. But when I do something like myDate.getTime() the timestamp returned was something like 4AM GMT time.
Why is this? And how do I get the date as midnight in UTC time?
At least in Chrome, this works:
var myDate = new Date('2013-01-01 00:00:00 UTC');
It also works if you put GMT instead of UTC. But I don't know if this is cross-browser enough.
I live in India. Hence my timezone is the Indian Standard Time (IST) which is listed in the tz database as Asia/Kolkata. India is 5 hours 30 minutes ahead of GMT. Hence when I execute new Date("2013-01-01 00:00:00") the actual time at GMT is "2012-12-31 18:30:00".
I believe you live in America because you're in the EST timezone (GMT-04:00)? Am I right?
If you want to parse the time at GMT instead of your local timezone then do this:
new Date("2013-01-01T00:00:00+00:00");
Notice the capital T between the date and the time, and the +00:00 at the end. This is the format used to parse a given time in a specific timezone.
Given the date string "2013-01-01 00:00:00" you can convert it to the required format using the following function:
function formatDateString(string, timezone) {
return string.replace(" ", "T") + timezone;
}
Then you can create the date as follows:
new Date(formatDateString("2013-01-01 00:00:00", "+00:00"));
Another way to convert local time to GMT is as follows:
var timezone = new Date("1970-01-01 00:00:00"); // this is the start of unix time
Now that you have your own local timezone as a date object you can do:
new Date(new Date("2013-01-01 00:00:00") - timezone);
All the above methods produce the same date at GMT.
JS reads this with time zone that your computer uses.
You can try use myDate.toUTCString() for get date in UTC time.
If you want get timestamp use myDate.getTime()
Mine works simply by doing this
var datetime= new Date()
However the month is 1 low so you have to add one