In this paper, we describe the implementation method of JS Strtotime () and date () function in PHP. Share to everyone for your reference. Specifically as follows:
In JS does not like PHP strtotime () and date () function, can directly convert the timestamp, the following we come from a function to implement JS specific timestamp conversion function.
function Datetime_to_unix (datetime) {
var tmp_datetime = datetime.replace (/:/g, '-');
Tmp_datetime = tmp_datetime.replace (//g, '-');
var arr = tmp_datetime.split ("-");
var now = new Date (DATE.UTC (arr[0],arr[1]-1,arr[2],arr[3]-8,arr[4],arr[5]));
Return parseint (Now.gettime ()/1000);
}
function Unix_to_datetime (Unix) {
var now = new Date (parseint (Unix) * 1000);
Return now.tolocalestring (). Replace (/year | month/g, "-"). Replace (/day/g, "");
}
var datetime = ' 2012-11-16 10:36:50 ';
var Unix = Datetime_to_unix (datetime);
document.write (datetime+ ' converted timestamp: ' +unix+ '
);
var unix = 1353033300;
var datetime = Unix_to_datetime (Unix);
document.write (unix+ ' converted date is: ' +datetime ');
If you want to eject: 2010-10-20 10:00:00 This format is good to do
<script>
function Getlocaltime (NS) {return
new Date (parseint (NS) * 1000). toLocaleString (). Replace (/year | Month/g, "-"). Replace (/day/g, "");
}
Alert (Getlocaltime (1177824835));
</script>
Full instance
<script type= "Text/javascript" > var day1 = parseint (new Date (). valueof ()/1000);
var day2 = new Date (day1 * 1000);
function Getlocaltime (NS) {return new Date (parseint (NS) * 1000). toLocaleString (). Replace (/:d {1,2}$/, ");
/* with the above function */functions Getlocaltimes (NS) {return new Date (parseint (NS) * 1000). toLocaleString (). substr (0,17);} function Getlocalformattime (NS) {return new Date (parseint (NS) * 1000). toLocaleString (). Replace (/year | month/g, "-"). Replace (/
Day/g, ""); document.getElementById ("Btn1"). onclick = function () {alert (day1);} document.getElementById ("Btn2"). onclick = function () {alert (day2.tolocalestring ());} document.getElementById ("Btn3"). onclick = function () {alert (getlocaltime
(Day1)); } document.getElementById ("Btn4"). onclick = function () {alert (Getlocalformattime (Day1));} document.getElementById (" Btn5 "). onclick = function () {alert (day2.getfullyear () +"-"+ (Day2.getmonth () +1) +"-"+day2.getdate () +" "+day2.gethours () + ":" +day2.getminutes () + ":" +day2.getsecoNDS ()); } </script>
The
wants this article to help you with your JavaScript programming.