JS imitates php strtotime () and date () function realization Method _javascript skill

Source: Internet
Author: User
Tags datetime

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.