Copy PHP strtotime () and date () functions in JS

Source: Internet
Author: User
Tags datetime
  code is as follows copy code

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

The code is as follows Copy Code

<script>
function Getlocaltime (NS) {
return new Date (parseint (NS) * 1000). toLocaleString (). Replace (/year | month/g, "-"). Replace (/day/g, "");
}
Alert (Getlocaltime (1177824835));
</script>

Full instance

The code is as follows Copy Code

<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 * *
function 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>

Related Article

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.