Using Heroku, I am trying to compare a saved times (open and close) of a place to the current time to determine what to output.
Code:
def get_place_search_display(open_time, close_time)
open_time_formatted = open_time.strftime('%l:%M %p') if !open_time.nil?
close_time_formatted = close_time.strftime('%l:%M %p') if !close_time.nil?
current_time = Time.current #.strftime('%l:%M %p')
if open_time.nil? || close_time.nil?
"Closed today"
elsif current_time < open_time
"Opens at #{open_time} until #{close_time}"
elsif current_time >= open_time && current_time <= close_time
"Open #{open_time} - #{close_time}"
elsif current_time > close_time
"Closed at #{close_time} today"
else
"Open until #{close_time} today"
end
end
Example returned values:
Open time: 7:00 AM
Close time: 8:45 PM, 2000-01-01 20:45:00 UTC
Current time: 6:35 PM, 2014-09-07 18:35:36 -0700
Returns: "Closed at 2000-01-01 20:45:00 UTC today"
Any ideas what the comparison isn't working using this logic?
Looking at the term "today" in your output string, and also the '%l:%M %p' in your time formatting method, I assume you are trying to compare Time objects without Date info
However in Ruby, Time object always comes with date. But you can achieve that by converting them into the same day before comparing them.
a = Time.current
=> Mon, 08 Sep 2014 02:30:44 UTC +00:00
b = Time.current.months_ago(1)
=> Fri, 08 Aug 2014 02:30:59 UTC +00:00
a > b
=> true
a2 = a.change(day:1, month:1, year:2000)
=> Sat, 01 Jan 2000 02:30:44 UTC +00:00
b2 = b.change(day:1, month:1, year:2000)
=> Sat, 01 Jan 2000 02:30:59 UTC +00:00
a2 > b2
=> false
Hope it helps.
Related
I have a string that I need to insert into a table as a timestamp.
'Thu Jul 02 13:36:17 UTC 2020'
Without UTC the following conversion works
SELECT to_timestamp('Thu Jul 02 13:36:17 2020', 'Dy Mon dd HH24:MI:SS yyyy');
How can I convert the timestamp with the UTC portion?
Any help would be greatly appreciated!
Database Type: PostgreSQL
Table Data Type: timestamp (this can change if needed)
If it's always going to be UTC, then you can use:
> select to_timestamp(
'Thu Jul 02 13:36:17 UTC 2020',
'Dy Mon DD HH24:MI:SS UTC YYYY'
);
Date startDate = new Date(Long.valueOf(""05/07/2018")getValue().toString());
get last day of selected month is required
private Date lastDayOfMonth(Date month) {
Date lastDay = (Date) month.clone();
CalendarUtil.addMonthsToDate(lastDay, 1);
CalendarUtil.setToFirstDayOfMonth(lastDay);
CalendarUtil.addDaysToDate(lastDay, -1);
GWT.log("lastDay :: "+lastDay);
return lastDay;
}
I get lastDay :: Tue Jul 31 00:00:00 GMT+530 2018
But i get 30th instead of 31st
First, your code does not work
Date startDate = new Date(Long.valueOf(""05/07/2018")getValue().toString());
Second, your method private Date lastDayOfMonth(Date month) looks fine, I have a test and got result Tue Jul 31 00:00:00
I dont understand your problem "Tue Jul 31 00:00:00 GMT+530 2018 But i get 30th instead of 31st". How can you get 30th from Tue Jul 31 00:00:00 GMT+530?
When I try to subtract
Wed Dec 06 2017 15:58:59 GMT+0530 (India Standard Time) minus Tue Nov 28 2017 00:00:00 GMT+0530 (India Standard Time) , the answer which is coming is -22
But the answer should be 6
What is going wrong and where, below is my page.ts code:
this.tt = new Date();
this.tt1 = this.datePipe.transform(this.tt,'dd/mm/yyyy');
console.log(this.ent[0],"server DATE");
// in console we see this - 28-NOV-17 server DATE
var firstDate= new Date(this.ent[0]); //Jan 01 2017 00:00:00
var secondDate = new Date();//Jan 04 2017 00:00:00
console.log(firstDate);
// answer in console - Tue Nov 28 2017 00:00:00 GMT+0530 (India Standard Time)
console.log(secondDate);
//answer in console - Wed Dec 06 2017 15:58:59 GMT+0530 (India Standard Time)
console.log(secondDate.getDate() - firstDate.getDate() );
//answer in console - -22
Date.getDate() gives the "dd" of the date (in your case 6 and 28 which explains the result being -22).
I'm a bit confused with the expected result being 6. So maybe my answer won't fit you. What I would do however is convert the date in time, do the substraction and convert it back to number of days.
So
Math.Floor((secondDate.getTime() - firstDate.getTime()) / 86400000);
(86400000 being 1000 (milliseconds) * 3600 (seconds in an hour) * 24 (number of hours in a day)
you can convert both dates to timestamp and subtract the timestamp you will get the result days in millis now convert it to days
getTimestamp(dateParam:string):string{
var date = new Date(dateParam); // some mock date
var milliseconds = date.getTime();
return milliseconds.toString();
}
var one_day=1000*60*60*24;
console.log(Math.ceil(getTimestamp(secondDate) - getTimestamp(firstDate))/(one_day) );
If I have two ISODates such as:
Tue Sep 18 1984 00:00:00 GMT+0100 (CET)
and
Sat Jun 21 2014 10:00:00 GMT+0100 (CET)
how do I get a difference between them using the mongo console? Specifically the difference in years?
they are from different collections so I can't use an aggregation for this.. :(
ISODate() is just a convenient wrapper around a standard JavaScript Date object so you can use the standard Date methods or calculate the difference yourself (date values are stored in milliseconds):
> var date1 = ISODate("1984-09-18");
> var date2 = ISODate("2014-06-21");
> date2.getFullYear() - date1.getFullYear()
30
> var yearMS = 365 * 24 * 60 * 60 * 1000; // a year in milliseconds
> parseFloat((date2-date1)/yearMS).toFixed(2)
29.78
In Go I'm trying to use the time.Parse() function from the time package to convert a string timestamp into a Time object. I know Go has an uncommon way of representing the time format your timestamps are in by providing it with an example of how their reference time (Mon Jan 2 15:04:05 -0700 MST 2006) would be displayed in your format. I'm still having issues with errors however. Here is an example of one of my timestamps:
Tue Nov 27 09:09:29 UTC 2012
Here is what the call I'm making looks like:
t, err := time.Parse("Mon Jan 02 22:04:05 UTC 2006", "Tue Nov 27 09:09:29 UTC 2012")
So basically what I've done here is try and match the formatting for day name/month name/day number, the hour/minute/second format, the string literal "UTC" and the year format. Note that I've increased the hours field of the Go reference format by 7 (from 15 to 22) to account for the fact that their timestamp is in a negative 7 timezone and all my timestamps are in a UTC timezone.
The error I get is:
parsing time "Tue Nov 27 09:09:29 UTC 2012" as "Mon Jan 02 22:04:05 UTC 2006": cannot parse ":09:29 UTC 2012" as "2"
What am I doing wrong here? Am I misinterpreting how to use time.Parse() or is my use case not supported for some reason?
Your format string should be:
Mon Jan 02 15:04:05 MST 2006
playground
That is, use MST for the timezone and 15 for the hour, as documented in your linked Parse function.
In this case, you can use time.UnixDate:
package main
import (
"fmt"
"time"
)
func main() {
t, e := time.Parse(time.UnixDate, "Tue Nov 27 09:09:29 UTC 2012")
if e != nil {
panic(e)
}
fmt.Println(t)
}
https://golang.org/pkg/time#UnixDate