I am trying to insert local time into database using javascript. Can any one help me?
could you please have a look on the file named member.class.php and please let me know where I have to put the javascript code... https://www.dropbox.com/sh/lfef67brewx7rub/wYRP72bDh7
Its very simple man...try with this
var milliseconds = new Date().getTime();
it will give you time in milli seconds or you can use like
new Date().getTime();
if you want it in unix(linux) timestamp then use like this
var unix = Math.round(+new Date()/1000); it will also give you in seconds
var currentdate = new Date();
var datetime = "Date n Tym : " + currentdate.getDate() + "/"
+ (currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear() + " # "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
10 ways to format time and date using JavaScript
See this answer!
In Javascript:
var d = new Date();
var date_with_time = d.getDate()+"-"
+d.getMonth()+1+"-"
+d.getFullYear()+" "
+d.getHours()+" "
+d.getMinutes()+" "
+d.getSeconds();
In PHP, the best way will be that you should create your time column DATETIME type and set default value CURRENT_TIMESTAMP. No need to manually insert it through Javascript.
DB like time stamp in js:
var timestamp = "" + d.getFullYear()
+ ("0" + (d.getMonth()+1)).slice(-2)
+ ("0" + d.getDate()).slice(-2) + "-"
+ ("0" + d.getHours()).slice(-2)
+ ("0" + d.getMinutes()).slice(-2)
+ ("0" + d.getSeconds()).slice(-2);
after this, for example, timestamp="20160408-134304"
Related
I have following code in my form. I need to let user input date and time.
<input type="datetime-local" class="form-control" id="collection_datetime" name="collection_datetime" required>
When trying to automatically test form with Laravel Dusk I will always get an error.
This is what I tried this
->type('collection_datetime', Carbon::now()->format('Y-m-d\TH:i'))
->type('collection_datetime', '12/31/2019 12:59 PM')
non of it worked.
Any suggestion you can give me is appreciated.
Thank you
This is what worked, I hope it will help someone.
It seems to me non-trivial I hope there is prettier solution
->type('collection_datetime', '13')
->type('collection_datetime', 'sep')
->keys('#collection_datetime', ['{tab}'])
->type('collection_datetime', '2019')
->keys('#collection_datetime', ['{tab}'])
->type('collection_datetime', '0104AM')
Setting datetime-local input using javascript in Laravel Dusk:
$browser->script('
var dateString = "10/4/22 7:9:00 PM"
if (dateString !== "") {
var dateVal = new Date(dateString);
var day = dateVal.getDate().toString().padStart(2, "0");
var month = (1 + dateVal.getMonth()).toString().padStart(2, "0");
var hour = dateVal.getHours().toString().padStart(2, "0");
var minute = dateVal.getMinutes().toString().padStart(2, "0");
var sec = dateVal.getSeconds().toString().padStart(2, "0");
var ms = dateVal.getMilliseconds().toString().padStart(3, "0");
var inputDate = dateVal.getFullYear() + "-" + (month) + "-" + (day) + "T" + (hour) + ":" + (minute);
$("#deadline").val(inputDate);
}
');
I have one problem, which I can not resolve and have no idea where is mistake
One I create a tabel called posts and in codeigniter create a form Posts, after createing all post I want to display posts in index.php and I have table called
Date of departure, and date of return. One i create post date looking good but when I display in my index.php it's look like 0000-00-00 00:00:00
In begginig data type was Date, now i change to be DateTime but nothing happend, same problem
Maybe I make mistake in my JS script
Any comment ?
My script
<script>
$( function() {
$( "#datepicker" ).datepicker();
$( "#datepicker1" ).datepicker();
} );
</script>
When your field in MySQL is Date-time it aspects proper format before saving in database.
So convert string to date time format in php using.
date("Y-m-d H:i:s", strtotime($string));
Or you can do the same in Datepicker
$(function() {
$('#datepicker').datepicker({
dateFormat: 'yy-dd-mm',
onSelect: function(datetext){
var d = new Date(); // for now
var h = d.getHours();
h = (h < 10) ? ("0" + h) : h ;
var m = d.getMinutes();
m = (m < 10) ? ("0" + m) : m ;
var s = d.getSeconds();
s = (s < 10) ? ("0" + s) : s ;
datetext = datetext + " " + h + ":" + m + ":" + s;
$('#datepicker').val(datetext);
},
});
});
Fiddle for datepicker
Note if you want date only in MySQL field the omit hour min sec part of conversion
I have created a timer to be used as a reference on a alert when the timer reaches exactly 5:00 pm/ 17:00. Do you have any ideas how could I do that? Do I check the time once in a while?
Here's how I create my timer.
<script language="JavaScript">
var tick;
function stop() {
clearTimeout(tick);
}
function usnotime() {
var datetoday = document.getElementById("datenow")
var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
datetoday.val("" + month + "/" + day + "/" + year + "");
var txt = document.getElementById("timeticker")
var ut = new Date();
var h, m, s;
var time = " ";
h = ut.getHours();
m = ut.getMinutes();
s = ut.getSeconds();
if (s <= 9) s = "0" + s;
if (m <= 9) m = "0" + m;
if (h <= 9) h = "0" + h;
time += h + ":" + m + ":" + s;
txt.innerHTML = time;
tick = setTimeout("usnotime()", 1000);
}
//--> document.rclock.rtime.value=time; tick=setTimeout("usnotime()",1000); } //-->
</script>
<tr>
<td class="tdlabel">Current Time:</td>
<td>
<div id="timeticker" name="rtime" size="22"></div>
</td>
</tr>
And it seems that my timer is not displaying. Any ideas??
You have a mistake, you mixed javascript and jquery.
datetoday.val("" + month + "/" + day + "/" + year + "");
Should be (assuming datetoday is a textfield. If div use innerHTML instead of value)
datetoday.value="" + month + "/" + day + "/" + year + "";
and add just before the closing script tag
window.onload=function(){
usnotime();
}
Not sure if this is the exact answer; it might help posting a bit more code.
I don't see how you're starting the method. You might need to add a button-execute / page-load call of usnotime().
Do you have real objects for all of those variables? (I.e. timeticker, datenow).
Check your JavaScript error log in the browser. It's a bit hard without more code for me to see what's wrong, so either include more, or use the debugger.
Using the setTimeout() u can do , Refer this it may be useful for you
This may help you....
<html>
<head>
<title>(Type a title for your page here)</title>
<script type="text/javascript">
function display_c(){
var refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('display_ct()',refresh)
}
function display_ct() {
var strcount
var x
var x1
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var seconds= currentTime.getSeconds()
x=month + "/" + day + "/" + year
if (minutes < 10){
minutes = "0" + minutes
}
x=x+" "+hours + ":" + minutes +":"+seconds+ " "
x1=hours + ":" + minutes + ":"+seconds+" "
if(hours > 11){
x=x+"PM"
x1=x1+"PM"
if(x1=="5:30:0 PM"){alert(x1);}
} else {
x=x+"AM"
x1=x1+"AM"
if(x1=="8:16:0 AM"){alert(x1);}
}
document.getElementById('ct').innerHTML = x;
tt=display_c();
}
</script>
</head>
<body onload=display_ct();>
<span id='ct' ></span>
</body>
</html>
i am working on javascript which displays time based on location, that is if a user logs on from china it should display their local time or a user from india it should display their code.No matter what, i am not able to get the code.pls someone help.
You can get the users local time in JS with:
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if (minutes < 10)
minutes = "0" + minutes
document.write("<b>" + hours + ":" + minutes + " " + "</b>")
I currently have a javascript variable which records the current date and time like so:
var time_of_call;
time_of_call = new Date();
and I need to store it in a MySQL database. When I try to upload it, the column just appears blank but I'm not sure what I'm doing wrong. I know it's not a problem with the mysql query because I have tried entering different values and it works OK.
I have the column set to DATETIME and I am uploading the value unformatted. Could someone please explain what I need to do differently?
Thanks for any help
P.s. I can't use NOW() because I am using that to capture the time that the record is actually captured, and this time_of_call records the time a call actually comes in.
In JavaScript, the underlying value of a Date object is in milliseconds, while Unix servers (and MySQL internally) uses whole seconds.
To get the underlying value for a javascript date object:
var pDate = new Date();
var pDateSeconds = pDate.valueOf()/1000;
From here, you'll send it to the server... it is up to you whether or not to divide it by 1000, but it has to be done somewhere. From this value, you could just call something like PHP's date('Y-m-d H:i:s', $pDateSeconds); on it.
Or, you could just use the built-in function in MySQL:
$sql = 'UPDATE table_name
SET field_name=FROM_UNIXTIME('.$pDateSeconds.')
WHERE field_name='.$row_id;
You must convert your format. Also, you don't "upload" an object.
At least, you have to do: time_of_call.getTime(); which returns a timestamp.
After uploading a timestamp, you have to convert to the DB's native format, eg: date('d-m-Y',(int)$_REQUEST['time_of_call']);
The date format depends on whether you used DATE, DATETIME, TIME, TIMESTAMP or INT.
If you used either TIMESTAMP or INT (which is best IMHO), you don't need any conversion.
Important: A javascript timestamp is in milliseconds, whereas a PHP timestamp is in seconds.
You will have to do time=time_of_call.getTime()/1000; to fix this.
Afaik Date doesn't add leading zero's to the day and month you should format the date like this:
yyyy-mm-dd hh:mm:ss
To get the expected result you could use a function:
Javascript:
function formatDate(date1) {
return date1.getFullYear() + '-' +
(date1.getMonth() < 9 ? '0' : '') + (date1.getMonth()+1) + '-' +
(date1.getDate() < 10 ? '0' : '') + date1.getDate();
}
You could use either of these to add a method for getting SQL formatted timestamps from a JS Date object.
First in user's local time:
Date.prototype.getTimestamp = function() {
var year = this.getFullYear(),
month = this.getMonth(),
day = this.getDate(),
hours = this.getHours(),
minutes = this.getMinutes(),
seconds = this.getSeconds();
month = month < 10 ? "0" + month : month;
day = day < 10 ? "0" + day : day;
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
}
var d = new Date();
console.log(d.getTimestamp());
Second in UTC:
Date.prototype.getUTCTimestamp = function() {
var year = this.getUTCFullYear(),
month = this.getUTCMonth(),
day = this.getUTCDate(),
hours = this.getUTCHours(),
minutes = this.getUTCMinutes(),
seconds = this.getUTCSeconds();
month = month < 10 ? "0" + month : month;
day = day < 10 ? "0" + day : day;
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
}
var d = new Date();
console.log(d.getUTCTimestamp());
I've created a lightweight script that extends the Date object with these and other commonly needed here: https://github.com/rtuosto/js-date-format
You need to add date with correct format, which is: YYYY-MM-DD HH:MM:SS. 10.3.1. The DATETIME, DATE, and TIMESTAMP Types
So to convert that you need to write something like this:
var t = new Date();
var YYYY = t.getFullYear();
var MM = ((t.getMonth() + 1 < 10) ? '0' : '') + (t.getMonth() + 1);
var DD = ((t.getDate() < 10) ? '0' : '') + t.getDate();
var HH = ((t.getHours() < 10) ? '0' : '') + t.getHours();
var mm = ((t.getMinutes() < 10) ? '0' : '') + t.getMinutes();
var ss = ((t.getSeconds() < 10) ? '0' : '') + t.getSeconds();
var time_of_call = YYYY+'-'+MM+'-'+DD+' '+HH+':'+mm+':'+ss;
Of course you can shorten all this and stuff like that, but you get the idea.
Try This:
Quick Java Script Date function :-)
/***************************************************
* Function for Fetch Date, Day, Time, Month etc..
* input #param = month, date, time, mysql_date etc..
**************************************************/
function getDateNow(output) {
var dateObj = new Date();
var dateString = dateObj.toString();
dateArray = dateString.split(” “);
if (output == ‘day’) {
output = dateArray[0];
} else if (output == ‘month’) {
output = dateArray[1];
} else if (output == ‘date’) {
output = dateArray[2];
} else if (output == ‘year’) {
output = dateArray[3];
} else if (output == ‘time’) {
output = dateArray[4];
} else if (output == ‘am_pm’) {
output = dateArray[5];
} else if (output == ‘mysql_date’) {
var dt = new Date();
output = dt.toYMD();
}else {
output = dateArray[6];
}
return output;
}
/*****************************************************
* Function for Fetch date like MySQL date fromat
* type #Prototype
****************************************************/
(function() {
Date.prototype.toYMD = Date_toYMD;
function Date_toYMD() {
var year, month, day;
year = String(this.getFullYear());
month = String(this.getMonth() + 1);
if (month.length == 1) {
month = “0″ + month;
}
day = String(this.getDate());
if (day.length == 1) {
day = “0″ + day;
}
return year + “-” + month + “-” + day;
}
})();
/***********************************
* How to Use Function with JavaScript
**********************************/
var sqlDate = getDateNow(‘mysql_date’));
/***********************************
* How to Use Function with HTML
**********************************/
<a href=”javascript:void(0);” onClick=”this.innerHTML = getDateNow(‘mysql_date’);” title=”click to know Date as SQL Date”>JavaScript Date to MySQL Date</a>
I found a snippet a while back, which may help depending on where you want to convert:
function formatDate(date1) {
return date1.getFullYear() + '-' +
(date1.getMonth() < 9 ? '0' : '') + (date1.getMonth()+1) + '-' +
(date1.getDate() < 10 ? '0' : '') + date1.getDate();
}