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.
Related
I have a Model Charter that hasMany BlackoutRanges. I am trying to setup a scope to return charters that do not have a blackoutRange created for 3 dates. In our system a blackoutRange is a single day.
Pseudocode would look something like this:
// query where doesnt have blackoutDates on all three of the following dates:
// start date === date passed in
// start date === date passed in plus 1 days
// start date === date passed in plus 2 days
I tried to do some logical grouping and came up with this and I also logged my raw sql query to see what it looks like:
$dayTwo = $date->copy()->addDays(1);
$dayThree = $date->copy()->addDays(2);
return $query->whereDoesntHave("blackoutRanges", function(Builder $subQuery) use($date, $dayTwo, $dayThree){
$temp = $subQuery->where(function($queryOne) use($date) {
return $queryOne->whereDate('start', $date)->where('trip_id', NULL);
})->where(function($queryTwo) use($dayTwo) {
return $queryTwo->whereDate('start', $dayTwo)->where('trip_id', NULL);
})->where(function($queryThree) use($dayThree) {
return $queryThree->whereDate('start', $dayThree)->where('trip_id', NULL);
});
logger($temp->toSql());
return $temp;
});
select * from `blackout_ranges` where `charters`.`id` = `blackout_ranges`.`charter_id` and (date(`start`) = ? and `trip_id` is null) and (date(`start`) = ? and `trip_id` is null) and (date(`start`) = ? and `trip_id` is null)
I've also logged the dates passed in and they are:
[2022-06-07 19:00:58] local.DEBUG: 2022-06-09 00:00:00
[2022-06-07 19:00:58] local.DEBUG: 2022-06-10 00:00:00
[2022-06-07 19:00:58] local.DEBUG: 2022-06-11 00:00:00
An example of the start column of a BlackoutRange would be: 2022-07-21 04:00:00
Would the fact that they are not matching timestamps be a reason for this scope not working?
And how do I know it's not working? I added blackoutRanges for a specific charter for the entire month of June and it's still being returned.
I am looking for any advice on this one as I've been plugging away now for a day and a half and have gotten no where.
I am using PHP with MySQL and would like to select rows that have a booking time within 2 hours from now. How do I compare what is in my database with the NOW() MySQL function?
I have columns pickupDate in the format yyyy-mm-dd and pickupTime in the format HH:mm (24-hour). I have tried creating a query with NOW() which returns the a 12-hour time as HH:mm:ss e.g. 2019-05-24 07:54:06 . I can't figure out how to format this to 19:54, or if I should use a different function instead.
For example, if the current date and time is 24/05/19 19:54:06, I would like to select rows between 19:54 and 21:54 on this date.
My table structure is:
referenceNo VARCHAR(100)
pickupDate DATE
pickupTime VARCHAR(100)
You need to create a DATETIME compatible value out of your pickupDate and pickupTime (which you can do by CONCATing them together), then you can compare that with a time range from NOW() to 2 hours later:
SELECT *
FROM yourtable
WHERE CONCAT(pickupDate, ' ', pickupTime) BETWEEN NOW() AND NOW() + INTERVAL 2 HOUR
Demo on dbfiddle
To add two hours in php
$hoursnow = date('H:i');
$timestamp = strtotime(date('H:i')) + 60*60*2;
$plusTwohours = date('H:i', $timestamp);
And $PlusTwohours using this variable frame the query like below
Sql Query:
$sqlQuery = 'select * from foodorder where pickupDate=DATE(NOW()) AND pickupTime>='.$hoursnow.' and pickupTime<='.$plusTwohours;
$result = mysql_query($sqlQuery);
variable $result will have the values of query
For Second Scenario: Adding hours to end of the day May 24 23:30:00
This should be handle by two different date for same column pickupDate
$d = new DateTime('2011-01-01 23:30:30');
$startDate = $d->format('Y-m-d H:i:s'); // For testing purpose assigned manually
$starttime = date('H:i');
// Here Process start, storing end date by adding two hours
$enddate1 = strtotime($startDate) + 60*60*2;
$enddate = date('Y-m-d', $enddate1); // Extracting date alone
$endtime = date('H:i', $enddate1); // Extracting time alone
Have to compare start and end date for column pickupDate, here is the query
$sqlQuery = "select * from foodorder where pickupDate>=DATE(".$startDate.") AND pickupDate<=DATE(".$enddate.") AND pickupTime>='".$starttime."' AND pickupTime<='".$endtime."'";
$result = mysql_query($sqlQuery);
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
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;
PreparedStatement pStmt = con.prepareStatement("insert into mydate values(?,?)");
java.util.Date now = new java.util.Date();
pStmt.setDate( 1, new java.sql.Date( now.getTime()) );
pStmt.setTimestamp( 2, new java.sql.Timestamp( now.getTime() ) );
pStmt.executeUpdate();
QUERY1:
I am creating post a comment module .I want to display the time when user posted their comments.also i want to show the time passed after they have posted a comment.
like 2 days ago ,5 min before etc.
QUERY2:
i also want to change the format of the date to dd/mm/yyyy.
For displaying the date formatted you can use java.text.SimpleDateFormat or
org.apache.commons.lang.time.FastDateFormat
For the time intervals you can use org.apache.commons.lang.time.DurationFormatUtils
Here is a sample:
//this will get the date in format dd/mm/yyyy
FastDateFormat fmt = FastDateFormat.getInstance("dd/MM/yyyy");
String txt = fmt.format(date);
//this will get the interval
long ival = System.currentTimeMilis()-date.getTime();
String interval = DurationFormatUtils.formatDurationWords(ival, true, true);