Understanding basic use of moment for date checking - time

Please I am new to using moments, and don't fully understand how utc and isBefore methods work.
I have the below code which returns false, I need help understanding why even though logically it should be true since 2021-07-11 is before 2021-07-12
var moment = require('moment');
moment.utc('2021-07-11').isBefore('2021-07-12', 'day') // returns false
Any pointer to resource or why false is returned would be greatly appreciated.

What is your local timezone?
You are comparing local 12th, to UTC 11. If your timezone is in the western hemisphere, then this is the expected result due to the 'day' parameter. When a timezone is not specified, momentJS will assume the local timezone.
The whole point of momentjs is to take timezones out of the equation for comparisons, so .isBefore() will return true only when the day component of the value passed in is less than the starting value when both values are transposed to the same timezone.
In this case, the day component of your two dates is equal and there before before will return false.
If your local timezone is UTC +2, then we can break your code down like this:
var utc = moment.utc('2021-07-11'):
var local = moment('2021-07-12');
// Local in UTC : 2021-07-11T22:00:00Z
utc.isBefore(local, 'day') // False
utc.isEqual(local, 'day') // True

Related

Change timezone with Carbon

I have a date that is coming from database (type date):
2018-08-25
This date is in french timezone.
When I do:
$from = Carbon::parse("2018-08-25", 'Europe/Paris');
$from->timezone('UTC');
dd($from);
I get:
date: 2018-08-24 22:00:00.0 UTC (+00:00)
Which is what I want
But when I use field from DB:
$operation = Operation::findOrFail($request->operation);
$from = Carbon::parse($operation->date_ini, 'Europe/Paris');
$from->timezone('UTC');
dd($from);
I get:
date: 2018-08-25 00:00:00.0 UTC (+00:00)
In my DB, field is saved as : 2018-08-25, so literraly, it means 2018-08-25 UTC. So result is coherent. But I'm not sure how to deal with it to get what I want. The implication would be that I have to store my date like a datetime in DB so that I can store it in UTC with 1 or 2 hours less. Is there anyway to avoid this and keep it simple ?
Any idea ?
I solved it using:
$from = Carbon::parse($operation->date_ini)->shiftTimezone('Europe/Paris');;
shiftTimezone with change timezone without changing the date. So, it do the trick for me !
If you call setTimezone() on an existing instance of Carbon, it will change the date/time to the new timezone, for example
$changeTimeZone = \Carbon\Carbon::parse($operation->date_ini)->setTimezone('Asia/Dhaka')->format('H:i');

Converting javascript time with php Carbon returns strange time?

I'm setting my time using javascript when creating/updating my cookie.
// get current time
let d = new Date();
let time = d.getTime();
Which variable time becomes this string...
1586186947954
The number above should result 16:29 GMT 6 April 2020
But when I run this number with carbon...
use Carbon\Carbon;
$updated = Carbon::parse($cart['updated']['time']);
This is returned...
Carbon\Carbon Object
(
[date] => 52234-04-07 05:32:34.000000
[timezone_type] => 1
[timezone] => +00:00
)
Which is way off for reason. Minutes and the date are no where near.
I'm trying to output this timestamp in the timezone Asia/Dubai too if I can get Carbon to return the right time anyway.
Any ideas would be great thanks.
The parse method is for parsing more complex strings, so it is misinterpreting the value you're passing.
You should instead construct the object like this:
Carbon::createFromTimestampMs(1586186947954, 'Asia/Dubai')

Spring data mongodb: Work with dates, date has 2 hours of difference

I've this document stored in mongo:
{
"_id" : "cpd4-734fc2db-a5b0-4881-b5d7-bf85d894178d",
"expiresAt" : ISODate("2018-10-10T00:00:00Z")
}
In order to get sure, all data is right, I've got first the document and I've log some data:
Reference ref = this.mongoTemplate.findById("cpd4-734fc2db-a5b0-4881-b5d7-bf85d894178d", Reference.class);
LOG.info(ref.getExpiresAt().toString());
LOG.info(Long.toString(ref.getExpiresAt().getTime()));
The result is:
Wed Oct 10 02:00:00 CEST 2018 <<<<<<<<<<<<<<<<<<<
1539129600000
As you can see when I get the object, the expiresAt field is 02:00:00 instead of 00:00:00.
Value in database is expiresAt field is: ISODate("2018-10-10T00:00:00Z")
Any ideas or thoughts welcome for this issue!
This date is in Zulu time (note the 'Z' on the end):
ISODate("2018-10-10T00:00:00Z")
When you do this, specifically the call to .toString(), you are converting the date into a local date string in your time zone, which appears to be Zulu+2:
LOG.info(ref.getExpiresAt().toString());
I usually set my server's time zone to UTC/Zulu/GMT, in order to avoid any automatic timezone conversions happening like this.

net.fortuna.ical4j.model.DateTime to ORACLE date

I have net.fortuna.ical4j.model.DateList which contains
net.fortuna.ical4j.model.Date objects
The output is: 20170522,20170523,20170525
(UTC time zone)
I have to convert it to ORACLE date in SystemDefault timeZone.
I tried to do this:
List<DATE> result = new ArrayList<DATE>
for(Date d : rdates){
result.add(new DATE(new Timestamp(d.getTime()));
}
But oracle date is different as expected.From net.fortuna.ical4j.model.Date 20170522,20170523,20170525 I got 20170521,20170522,20170524 ORACLE DATE.
There is shifting. How can I handle this?
Ical4j Date objects have an underlying timezone that is not defined as part of the formal specification (an implementation quirk).
By default this timezone will be UTC, however you can change this to system default using the following compatibility hint:
net.fortuna.ical4j.timezone.date.floating=true

Ruby on Rails 3 convert time to DateTime in specific timezone

I have column which stores only time. I want to convert to it into datetime and then to a specific timezone. When I do it on console using only the attribute, it shows correct conversion value. But when I use it in query the query shows the current time in UTC. Suppose I have time "05:15" stored in Central time. Now when I want to fetch records for a interval of 30 minutes plus and minus of this time, I do following,
day = Time.now
# departure_time_from _db below is the name of column in table which store time
departure = departure_time_from _db.change(:day => date.day, :month => date.month, :year => date.year)
departure = departure.in_time_zone("Central Time (US & Canada)")
When I execute above code on console and see the value, it shows correct converted value. But when I use this variable in below query, current time gets used instead of the departure time.
Model.where("column_name > ? and "column_name < ?, departure - 30.minutes, departure + 30.minutes)
The time used in above query should be the time I want i.e Central time zone, but in query output, it shows UTC being used.
Please let me know what i am missing.
It shouldn't matter what time zone the query uses in the where clause as long as it's the same time representation. The time you're seeing in your query output from Rails is most likely the UTC equivalent of the ruby DateTime object being passed to the where statement. So if departure is 12:00 noon (UTC -6) in your Ruby code then the time shown in the SQL query will be 18:00, which corresponds to noon CST.

Resources