was using postgres..
I got the columns similar like below
start_time: "2013-11-30 03:51:00"
But I having date which ruby generated
Date.today -> Mon, 16 Dec 2013
How do I trigger records that fall on the day?
select * from records where start_time = ?
Record.where("start_time = ?", Date.today)
how about comparing the date?
Record.where("start_time >= ? and start_time < ?", Date.today, Date.today + 1)
Date.today + 1 is the beginning of next day
Related
I am trying to retrieve dates from a date column in mySQL database. My code (mysqljs) :
today = document.getElementBYId("myDate").value
The result :
today = Thu Dec 23 2021 14:05:00 GMT+0800 (Singapore Standard Time)
How can I get it to return just the date and time (yyy:mm:dd hh:mm:ss)?
(In mySql - the column type of the field date is set to DATETIME.)
Finally I have fund a way to solve this problem.
First I capture the date into a variable :
`var today = document.getElementById("mydate").value;`
(Today is in the format :
Thu Dec 23 2021 14:05:00 GMT+0800 (Singapore Standard Time)
Then I split it (to remove those info that I do not want):
var t = today.split(" ");
var d = t[0]+" "+t[1]+" "+t[2]+","+t[3]
Next I create a function to format the date :
function formateDate(date){
var e = new Date(date),
month = '' + (e.getMonth() + 1),
day = '' + e.getDate(),
year = e.getFullYear();
if (month.length <2)
month = '0' + month;
if (day.length <2)
day = '0' + day;
return [year, month, day].join('-');
}
Then I pass today into the function :
`today_x = formatDate(d)`
Finally I display it to the form That I am rendering :
document.getElementById("mydate").value = today_x;
I hope this is helpful. I don't think this is the most efficient way but I have been struggling with this for almost a week and this is the only solution I can come up with. I hope someone can offer another solution that is less tedious.
I have table temp with structure on sqlfiddle:
id(int 11 primary key)
name(varchar 100)
name2(varchar 100)
date(datetime)
I would like get record on this week, example if now 21.11.2013 i would like all rows on 18.11.2013 to 24.11.2013(on week)
Now I see next algorithm:
obtain weekday
calculate how many days ago was Monday
calculate the date Monday
calculate future date Sunday
make a request on date
Tell me please, is exist a shorter algorithm (preferably in the query MySQL)?
ADD Question is: Why this query select record on date 17.11.2013(Sunday) - 23.11.2013(Saturday) and how get records on date 18.11.2013(Monday) - 24.11.2013(Sunday) ?
query:
select * from temp
where yearweek(`date`) = yearweek(curdate())
Thanks!
Use YEARWEEK():
SELECT *
FROM your_table
WHERE YEARWEEK(`date`, 1) = YEARWEEK(CURDATE(), 1)
Use YEARWEEK. If you use WEEKOFYEAR you will get records of previous years also.
SELECT id, name, date
FROM table
WHERE YEARWEEK(date)=YEARWEEK(NOW());
For selecting records of day, week and month use this way:
function my_func($time, $your_date) {
if ($time == 'today') {
$timeSQL = ' Date($your_date)= CURDATE()';
}
if ($time == 'week') {
$timeSQL = ' YEARWEEK($your_date)= YEARWEEK(CURDATE())';
}
if ($time == 'month') {
$timeSQL = ' Year($your_date)=Year(CURDATE()) AND Month(`your_date`)= Month(CURDATE())';
}
$Sql = "SELECT * FROM your_table WHERE ".$timeSQL
return $Result = $this->db->query($Sql)->result_array();
}
You can do it by following method
SELECT DATE_FORMAT("2017-06-15", "%U");
Get number of week (From 00 to 53 )
Where (Sunday 1st day and Monday last day of week)
May be useful for you.
example:
SELECT DISTINCT DATE_FORMAT('dates', '%U') AS weekdays FROM table_name;
The short solution would be this:
SELECT * FROM my_table WHERE DATE_FORMAT("2021-08-19 15:40:33", "%U") = WEEK(CURDATE());
I have table temp with structure on sqlfiddle:
id(int 11 primary key)
name(varchar 100)
name2(varchar 100)
date(datetime)
I would like get record on this week, example if now 21.11.2013 i would like all rows on 18.11.2013 to 24.11.2013(on week)
Now I see next algorithm:
obtain weekday
calculate how many days ago was Monday
calculate the date Monday
calculate future date Sunday
make a request on date
Tell me please, is exist a shorter algorithm (preferably in the query MySQL)?
ADD Question is: Why this query select record on date 17.11.2013(Sunday) - 23.11.2013(Saturday) and how get records on date 18.11.2013(Monday) - 24.11.2013(Sunday) ?
query:
select * from temp
where yearweek(`date`) = yearweek(curdate())
Thanks!
Use YEARWEEK():
SELECT *
FROM your_table
WHERE YEARWEEK(`date`, 1) = YEARWEEK(CURDATE(), 1)
Use YEARWEEK. If you use WEEKOFYEAR you will get records of previous years also.
SELECT id, name, date
FROM table
WHERE YEARWEEK(date)=YEARWEEK(NOW());
For selecting records of day, week and month use this way:
function my_func($time, $your_date) {
if ($time == 'today') {
$timeSQL = ' Date($your_date)= CURDATE()';
}
if ($time == 'week') {
$timeSQL = ' YEARWEEK($your_date)= YEARWEEK(CURDATE())';
}
if ($time == 'month') {
$timeSQL = ' Year($your_date)=Year(CURDATE()) AND Month(`your_date`)= Month(CURDATE())';
}
$Sql = "SELECT * FROM your_table WHERE ".$timeSQL
return $Result = $this->db->query($Sql)->result_array();
}
You can do it by following method
SELECT DATE_FORMAT("2017-06-15", "%U");
Get number of week (From 00 to 53 )
Where (Sunday 1st day and Monday last day of week)
May be useful for you.
example:
SELECT DISTINCT DATE_FORMAT('dates', '%U') AS weekdays FROM table_name;
The short solution would be this:
SELECT * FROM my_table WHERE DATE_FORMAT("2021-08-19 15:40:33", "%U") = WEEK(CURDATE());
document.getElementById("appealTriggerDate").valueAsDate = vm.appealEntity.DateOfAppealTrigger();
I am using above line of code to display date in HTML5 date control. After I do this, I find that I am unable to edit date from the date control. The control does not set the date selected.
Fri Nov 15 2013 00:00:00 GMT-0500(Eastern Standard Time)
vm.appealEntity.DateOfAppealTrigger() returns date in above format. The date is as fetched from the database. I am not doing any formatting.
If I try to set the date in the format below, I can see the date set in the control, but edit it still not possible.
var dt= new Date(vm.appealEntity.DateOfAppealTrigger());
var month = dt.getMonth() + 1;
var day = dt.getDate();
if (month < 10)
month = "0" + month;
if (day < 10)
day = "0" + day;
document.getElementById("appealTriggerDate").value = dt.getFullYear() + "-" + month + "-" + day;
I'm getting a weird return when executing this query :
SELECT * FROM rrp
WHERE end > "2012-12-31"
nothing is returned, although I have one row on this table which "end" column is greater than "2012-12-31":
rrp
id_r | id__b | start | end | quantity
27 29 2012-01-01 2012-05-05 1
31 29 2012-11-01 2013-01-01 1
EDIT : startand endare date fields
EDIT : I used wrong database for my tests => wrong result
the issue was coming from Zend_Date when adding a day to a date:
$start = "2012-12-31";
$nStart = new Zend_Date($start, "YYYY-MM-dd");
$end = new Zend_Date($nStart);
$end->addDay(1);
When i echoed $end : echo $end->get("YYYY-MM-dd");
it outputs 2013-12-31
Most likely an issue with how the dates are formatted
This should help
http://dev.mysql.com/doc/refman/5.0/en/using-date.html
If end is a DATE column, it should work as expected:
SELECT
STR_TO_DATE('2013-01-01', '%Y-%m-%d') < "2012-12-31",
STR_TO_DATE('2012-05-05', '%Y-%m-%d') < "2012-12-31"
... returns 0, 1 in my box.
The only possible flaw I can think of is that your system's default date format is not %Y-%m-%d:
SELECT ##DATE_FORMAT
In that case, you need to specify a format every time:
SELECT *
FROM rrp
WHERE end > STR_TO_DATE('2012-12-31', '%Y-%m-%d')