im having a problem with datetime in rails. Im reading a json and saving dates in a model. that has a column t.date:datetime
The thing is there is 2 dates that i get from this json, and i want only to save it as 1.
I get date for day : 2022-04-16 00:00:00 and also i get a hour date : 01:00:00 is there a way to add that hour date to my day date ? Like getting 2022-04-16 01:00:00 ?
Im new to rails so sorry if im not explaining correctly.
An hour is 1/24th of a day. DateTime happily let's you add that to a date.
t1 = DateTime.today
t2 = t1 + 1/24r # a rational number. 1.0/24 is fine too.
You can convert your time to second and then add it to your date
date = "2022-04-16 00:00:00".to_datetime
time = Time.parse("01:00:00").seconds_since_midnight.seconds
date + time
You don't need to do to_datetimeif your date is already a datetime object
Related
In my rails project I want to get a Date instance only by a day. Year and month are used current value.
I could write like these:
day = 3
date = Date.new(Date.current.year, Date.current.month, day)
and
date = Date.current.beginning_of_month + (day - 1).days
How would you write function like this?
Is there a better implementation?
If you already have a Date object, you can do:
date.change(day: 3)
where date is a Date or DateTime object. Also you can do:
Date.today.change(day: 3)
Informix is not my normal environment and the way it handles datetime values is throwing me for a loop. I can't imagine this is difficult, but for the life of me I'm not yet able to figure it out.
This is the SQL:
SELECT agentid,
extension As Ext,
resourcefirstname As FirstNm,
resourcelastname As LastNm,
Min(eventdatetime) As FirstIn
FROM agentstatedetail AS asdr Join
resource As r On asdr.agentid = r.resourceid
WHERE asdr.eventdatetime BETWEEN '2016-10-20 04:00:00' AND '2016-10-21 03:59:59'
AND eventtype = 3
AND assignedteamid = 14
Group By agentid, extension, resourcefirstname, resourcelastname
Order By Min(eventdatetime)
Everything works as is, but the dates in the Between clause are currently entered manually- not optimal. I just need some way to describe "yesterday at 4:00 AM" and "Today at 4:00 AM" Will somebody please clue me in?
Using Informix version 12.10.FC6DE, I can do this:
SELECT
TODAY::DATETIME YEAR TO SECOND AS today_zerohour
, TODAY::DATETIME YEAR TO SECOND - '20:00:00'::INTERVAL HOUR TO SECOND AS yesterday_dawn
, TODAY::DATETIME YEAR TO SECOND + '04:00:00'::INTERVAL HOUR TO SECOND AS today_dawn
FROM
systables
WHERE
tabid = 1;
And it returns:
today_zerohour yesterday_dawn today_dawn
2016-10-21 00:00:00 2016-10-20 04:00:00 2016-10-21 04:00:00
So, what is happening here:
The operator TODAY returns the system date as a DATE type. The DATE type does not have the precision I want (it only has year, month and day), so I cast the value (cast operator is ::) to a DATETIME with precision from year to second (the hour, minutes and seconds are set to zero):
TODAY::DATETIME YEAR TO SECOND
In Informix, for an addition or subtraction with a DATETIME value to return another DATETIME value, I need to add or subtract an INTERVAL value. So I created 2 INTERVAL values.
One INTERVAL of 20 hours to subtract from the today value (again the cast operator :: is used, this time to cast from a string to an INTERVAL):
'20:00:00'::INTERVAL HOUR TO SECOND
One INTERVAL of 4 hours to add to the today value:
'04:00:00'::INTERVAL HOUR TO SECOND
Background: I'm building an app with Angular JS as web interface and Rails API. The problem I am having is passing a date from Angular to Rails.
Issue: I have a form with a Date of Birth date field, when a user inputs his DOB say March 1st, 1985, Angular interprets it as 1985-03-01 00:00 +0800 (if you're in Hong Kong or Singapore) and sends a request to Rails. The first thing Rails does with it is to convert it to UTC, which means the datetime is now 1985-02-28 16:00 UTC. Therefore, when the date is saved to the database date column, it becomes Feb 28, 1985.
Solution for now: What I'm doing now is on Angular side, I get the Timezone offset hours and add it to the date, so instead of 1985-03-01 00:00 +0800, it is now 1985-03-01 08:00 +0800. When Rails get it, it converts to 1985-03-01 00:00 UTC and so saves the correct date to db. However, I believe this is a better alternative to tackle this issue.
Thinking about parsing just the date in Rails, yet the params[:dob] I see is already UTC by the time I get it. Would love to know if there is a better practice than my current solution. Thank you for any comment and feedback.
This problem is actually quite common, and stems from two separate but related issues:
The JavaScript Date object is misnamed. It's really a date + time object.
The JavaScript Date object always takes on the characteristics of the time zone for the environment in which it is running in.
For a date-only value like date-of-birth, the best solution to this problem is to not send a full timestamp to your server. Send just the date portion instead.
First, add 12 hours to the time, to use noon instead of midnight. This is to avoid issues with daylight saving time in time zones like Brazil, where the transition occurs right at midnight. (Otherwise, you may run into edge cases where the DOB comes out a day early.)
Then output the date portion of the value, as a string in ISO format (YYYY-MM-DD).
Example:
var dt = // whatever Date object you get from the control
dt.setHours(dt.getHours() + 12); // adjust to noon
var pad = function(n) { return (n < 10 ? '0' : '') + n; }
var dob = dt.getFullYear() + '-' + pad(dt.getMonth()+1) + '-' + pad(dt.getDate());
Another common way to do this is:
var dt = // whatever Date object you get from the control
dt.setHours(dt.getHours() + 12); // adjust to noon
dt.setMinutes(dt.getMinutes() - dt.getTimezoneOffset()); // adjust for the time zone
var dob = dt.toISOString().substring(0,10); // just get the date portion
On the Rails side of things, use a Date object instead of a DateTime. Unlike JavaScript, the Rails Date object is a date-only object - which is perfect for a date-of-birth.
How to get os.time from the data string with this format yyyy-MM-dd'T'HH:mm:ss.SSSZZZ and I will do some calculations to get date after number of days from the above date, but I need to get the resultant date also in this format yyyy-MM-dd'T'HH:mm:ss.SSSZZZ
I am able to do the calculations but the only issue is in need to read the date with the above format and return the date in the same format.
now = os.time{year = yearValue, month = monthValue, day = dayValue, hour = hourValue, min = minutesValue}
numberOfDays = now + 2 * 24 * 3600 -- here 2 is the number of days
print(os.date("%c",numberOfDays)) -- number of days returns the date after give number of days
Can anyone help me with the date format(yyyy-MM-dd'T'HH:mm:ss.SSSZZZ) that i should read and return. Thanks in advance
print(os.date("%Y-%m-%dT%H:%m:%S.000 %z",numberOfDays))
I think this is the closest you can get. milliseconds is hardcoded 000 because there are no milliseconds in lua and the timezone is like +0100 for example.
reference to date formatting used in lua: http://www.cplusplus.com/reference/ctime/strftime/
I'm trying to execute a query in rails 3 with the following syntax: CourseOrder.where("DATE(ordered_at) = ?", date).
My problem is that rails saves all times in UTC, so in my Timezone (+2) at 0:33 its the 4th but in UTC its the 3th of the month. Is there a way to query the date part of a DateTime with Timezone?
Don't use date, do it like this instead:
time = DateTime.civil_from_format(:local, 2011, 4, 9)
CourseOrder.where("ordered_at > ? AND ordered_at < ?", time - 1.day, time)
You're storing a datetime, so query that instead of trying to use date, which doesn't account for Timezones.
solved the problem with a change in which values I compare.
I have a Date value to, and now I'm comparing a date value with a date value which is better than comparing date value with datetime value which have to fail.
thanks for leading me to the right direction