Js imitating strtotime () and date () functions in php _ javascript tips-js tutorial

Source: Internet
Author: User
This article describes how to implement the strtotime () and date () functions in php in js imitation. It involves related techniques related to javascript time operations and is of great practical value, for more information about how to use js to simulate strtotime () and date () functions in php, see the following example. Share it with you for your reference. The details are as follows:

In js, there are no strtotime () and date () functions like php. You can directly convert the timestamp. Next we will define a function to implement specific timestamp conversion functions in js.

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 = '2017-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: '+ datetime );

If you want to pop up in the format of 10:00:00, you can do it.

Script function getLocalTime (nS) {return new Date (parseInt (nS) * 1000 ). toLocaleString (). replace (/year | month/g ,"-"). replace (/day/g, "");} alert (getLocalTime (1177824835); script

Complete instance

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.