Does javascript-JS have a method similar to PHPdate?

Source: Internet
Author: User
For example, you want to implement the function. Input the timestamp "1400000000". Return format: 2015-01-01PHP implementation: date (& quot; Y-m-dH: I: s & quot;, & quot; 1400000000 & quot ;); what about javascript implementation ?? For example, you want to implement the function.
Input timestamp "1400000000", return format:

PHP implementation: date ("Y-m-d H: I: s", "1400000000 ");
What about javascript implementation ??

Reply content:

For example, you want to implement the function.
Input timestamp "1400000000", return format:

PHP implementation: date ("Y-m-d H: I: s", "1400000000 ");
What about javascript implementation ??

The write is rough but can be implemented.

Function getFormatDate (split, timeInMs) {var curr = new Date (); curr. setTime (timeInMs); var year = curr. getFullYear (); var month = curr. getMonth () + 1; var date = curr. getDate (); var hours = curr. getHours (); var minutes = curr. getMinutes (); var seconds = curr. getSeconds (); var formatDateStr = year + split [0] + month + split [0] + date + "" + hours + split [1] + minutes + split [1] + seconds; return formatDateStr;} var res = getFormatDate (["-", ":"], 1433456000345); // enter the 13-digit console here. log (res); // 6:13:20

Https://github.com/jacwright/date.format

If you implement it by yourself, you can view the above ones and paste a plug-in.

Http://momentjs.cn/

Compatible with most of the date functions in PHP:

function date () {    if (arguments.length == 0) {        throw "date() expects at least 1 parameter, 0 given";    }    var format = arguments[0];    var time = arguments.length > 1 ? new Date(arguments[1] * 1000) : new Date();    var padding = function (str, length) {        return "0000".substr(0, length - str.toString().length) + str;    }    return format.replace(/[a-z]/ig, function (c){        var dayOfYear = function (time) {            return (time.getTime() - (new Date(time.getFullYear(), 0, 1)).getTime()) / 86400000 >> 0;        }        var daysOfMonth = function (time) {            var year = time.getFullYear();            var month = time.getMonth();            return ((new Date(year, month + 1, 1)).getTime() - (new Date(year, month, 1)).getTime()) / 86400000 >> 0;        }        var sign = function (num) {            return num >= 0 ? "+" : "-";        }        switch (c) {            case "d":                return padding(time.getDate());            case "D":                return ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][time.getDay()];            case "j":                return time.getDate();            case "l":                return ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][time.getDay()];            case "N":                return [7, 1, 2, 3, 4, 5, 6][time.getDay()];            case "S":                switch(time.getDate()) {                    case 1:                        return "st";                    case 2:                    case 22:                        return "nd";                    case 3:                    case 23:                        return "rd";                }                return "th";            case "w":                return time.getDay();            case "z":                return dayOfYear(time);            case "W":                return dayOfYear(time) / 7 >> 0;            case "F":                return ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][time.getMonth()];            case "m":                return padding(time.getMonth() + 1, 2);            case "M":                return ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][time.getMonth()];            case "n":                return time.getMonth() + 1;            case "t":                return daysOfMonth(time);            case "L":                return dayOfyear(new Date(time.getFullYear() + 1, 0, 1)) == 365;            case "Y":                return time.getFullYear();            case "y":                return padding(time.getFullYear() % 100, 2);            case "a":                return time.getHours() >= 12 ? "am" : "pm";            case "A":                return time.getHours() >= 12 ? "AM" : "PM";            case "g":                return time.getHours() % 12;            case "G":                return time.getHours();            case "h":                return padding(time.getHours() % 12, 2);            case "H":                return padding(time.getHours(), 2);            case "i":                return padding(time.getMinutes(), 2);            case "s":                return padding(time.getSeconds(), 2);            case "u":                return time.getTime() % 1000 + "000";            case "O":                return sign(-time.getTimezoneOffset()) + padding(Math.abs(time.getTimezoneOffset()) / 60, 2) + "00";            case "P":                return sign(-time.getTimezoneOffset()) + padding(Math.abs(time.getTimezoneOffset()) / 60 >> 0, 2) + ":" + Math.abs(time.getTimezoneOffset()) % 60;            case "Z":                return -time.getTimezoneOffset();            case "U":                return time.getTime() / 1000 >> 0;            case "o":            case "B":            case "e":            case "I":            case "T":            case "c":            case "r":                throw c + " is not yet implemented.";            default:                return c;         }    })}

// Extend the Date to String // month (M), Day (d), hour (h), and minute (m) in the specified format), second (s), quarter (q) can use 1-2 placeholders, // year (y) can use 1-4 placeholders, Millisecond (S) you can use only one placeholder (a number ranging from 1 to 3) function formatDate (date, fmt) {// author: meizz fmt = fmt | 'yyyy-MM-dd hh: mm: ss'; var o = {"M +": date. getMonth () + 1, // month "d +": date. getDate (), // Day "h +": date. getHours (), // Hour "m +": date. getMinutes (), // minute "s +": date. getSeconds (), // second "q +": Math. floor (date. getMonth () + 3)/3), // quarter "S": date. getMilliseconds () // millisecond}; if (/(y + )/. test (fmt) fmt = fmt. replace (RegExp. $1, (date. getFullYear () + ""). substr (4-RegExp. $1. length); for (var k in o) if (new RegExp ("(" + k + ")"). test (fmt) fmt = fmt. replace (RegExp. $1, (RegExp. $1. length = 1 )? (O [k]): ("00" + o [k]). substr ("" + o [k]). length); return fmt ;}

I copied it from other places.

var unixTimestamp = new Date(1446027232 * 1000);commonTime = unixTimestamp.toLocaleString();

I hope this will help you.

Yes, but it is not easy to use.
Additional language versions

Java timeJavaScript Math. round (new Date (). getTime ()/1000) getTime () returns a number in milliseconds Microsoft. NET/C # epoch = (DateTime. now. toUniversalTime (). ticks-621355968000000000)/10000000 MySQL SELECT unix_timestamp (now () Perl timePHP time () PostgreSQL SELECT extract (epoch FROM now () Python first import time and then time. time () Ruby gets Unix timestamp: Time. now or Time. new: Unix timestamp: Time. now. to_iSQL Server select datediff (s, '2017-01-01 00:00:00 ', GETUTCDATE () Unix/Linux date + % sVBScript/ASP DateDiff ("s", "01/01/1970 00:00:00 ", now () other operating systems (if Perl is installed in the System) command line status: perl-e "print time"

We recommend that you use momentjs, which has a low chance of making bugs. However, you can study the extensions written by yourself upstairs.

Https://segmentfault.com/q/1010000000701472/a-1020000005044252? Utm_source = Weibo
The upstairs code is good, and there is one here, which is similar.

function formatDate(now)   {                   var   year=now.getYear();                   var   month=now.getMonth()+1;                   var   date=now.getDate();                   var   hour=now.getHours();                   var   minute=now.getMinutes();                   var   second=now.getSeconds();                   return   year+"-"+month+"-"+date+"   "+hour+":"+minute+":"+second;      } 

You can try this function.
Just change it.
return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;

sdfsdfsd

sdfsadfsd

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.