将毫秒转换为日期(jQuery / JavaScript)

时间:2011-01-12 20:10:40

标签: javascript jquery datetime

我有点像漫步者,但我会尽力保持清晰 -

我很无聊,所以我正在制作一个“shoutbox”,我对一件事感到有些困惑。我希望得到一条消息输入的时间,我想确保我得到服务器时间,或者至少确保我没有得到用户的本地时间。我知道这没关系,因为除了我之外,任何人都不会使用这个东西,但我想要彻底。我环顾四周并测试了一些东西,我认为这样做的唯一方法就是从?/?/ 1970或者其他任何东西获得毫秒数,因为对每个人来说都是一样的。

我这样做是这样的:

var time = new Date();
var time = time.getTime();

返回类似1294862756114的数字。

有没有办法将1294862756114转换为更易读的日期,例如DD/MM/YYYY HH:MM:SS

所以,基本上,我正在寻找JavaScript相当于PHP的date();函数。

11 个答案:

答案 0 :(得分:259)

    var time = new Date().getTime();
    var date = new Date(time);
    alert(date.toString()); // Wed Jan 12 2011 12:42:46 GMT-0800 (PST)

答案 1 :(得分:144)

如果您想为自己的日期定制格式,我会为其提供a simple function

var now = new Date;
console.log( now.customFormat( "#DD#/#MM#/#YYYY# #hh#:#mm#:#ss#" ) );

以下是支持的令牌:

token:     description:             example:
#YYYY#     4-digit year             1999
#YY#       2-digit year             99
#MMMM#     full month name          February
#MMM#      3-letter month name      Feb
#MM#       2-digit month number     02
#M#        month number             2
#DDDD#     full weekday name        Wednesday
#DDD#      3-letter weekday name    Wed
#DD#       2-digit day number       09
#D#        day number               9
#th#       day ordinal suffix       nd
#hhhh#     2-digit 24-based hour    17
#hhh#      military/24-based hour   17
#hh#       2-digit hour             05
#h#        hour                     5
#mm#       2-digit minute           07
#m#        minute                   7
#ss#       2-digit second           09
#s#        second                   9
#ampm#     "am" or "pm"             pm
#AMPM#     "AM" or "PM"             PM

这是代码:

//*** This code is copyright 2002-2016 by Gavin Kistner, !@phrogz.net
//*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt
Date.prototype.customFormat = function(formatString){
  var YYYY,YY,MMMM,MMM,MM,M,DDDD,DDD,DD,D,hhhh,hhh,hh,h,mm,m,ss,s,ampm,AMPM,dMod,th;
  YY = ((YYYY=this.getFullYear())+"").slice(-2);
  MM = (M=this.getMonth()+1)<10?('0'+M):M;
  MMM = (MMMM=["January","February","March","April","May","June","July","August","September","October","November","December"][M-1]).substring(0,3);
  DD = (D=this.getDate())<10?('0'+D):D;
  DDD = (DDDD=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][this.getDay()]).substring(0,3);
  th=(D>=10&&D<=20)?'th':((dMod=D%10)==1)?'st':(dMod==2)?'nd':(dMod==3)?'rd':'th';
  formatString = formatString.replace("#YYYY#",YYYY).replace("#YY#",YY).replace("#MMMM#",MMMM).replace("#MMM#",MMM).replace("#MM#",MM).replace("#M#",M).replace("#DDDD#",DDDD).replace("#DDD#",DDD).replace("#DD#",DD).replace("#D#",D).replace("#th#",th);
  h=(hhh=this.getHours());
  if (h==0) h=24;
  if (h>12) h-=12;
  hh = h<10?('0'+h):h;
  hhhh = hhh<10?('0'+hhh):hhh;
  AMPM=(ampm=hhh<12?'am':'pm').toUpperCase();
  mm=(m=this.getMinutes())<10?('0'+m):m;
  ss=(s=this.getSeconds())<10?('0'+s):s;
  return formatString.replace("#hhhh#",hhhh).replace("#hhh#",hhh).replace("#hh#",hh).replace("#h#",h).replace("#mm#",mm).replace("#m#",m).replace("#ss#",ss).replace("#s#",s).replace("#ampm#",ampm).replace("#AMPM#",AMPM);
};

答案 2 :(得分:30)

您可以简单地使用Datejs库,以便将日期转换为所需的格式。

我经常参加考试,并且有效。

以下是一个说明如何实现这一目标的片段:

var d = new Date(1469433907836);

d.toLocaleString(); // expected output: "7/25/2016, 1:35:07 PM"

d.toLocaleDateString(); // expected output: "7/25/2016"

d.toDateString();  // expected output: "Mon Jul 25 2016"

d.toTimeString(); // expected output: "13:35:07 GMT+0530 (India Standard Time)"

d.toLocaleTimeString(); // expected output: "1:35:07 PM"

答案 3 :(得分:18)

以下是一个代码段,可让您将日期格式化为所需的输出:

var time = new Date();
var time = time.getTime();

var theyear = time.getFullYear();
var themonth = time.getMonth() + 1;
var thetoday = time.getDate();

document.write("The date is: ");
document.write(theyear + "/" + themonth + "/" + thetoday);

答案 4 :(得分:5)

尝试使用此代码:

var milisegundos = parseInt(data.replace("/Date(", "").replace(")/", ""));
var newDate = new Date(milisegundos).toLocaleDateString("en-UE");

享受它!

答案 5 :(得分:3)

尝试使用此代码:

var datetime = 1383066000000; // anything
var date = new Date(datetime);
var options = {
        year: 'numeric', month: 'numeric', day: 'numeric',
    };

var result = date.toLocaleDateString('en', options); // 10/29/2013

查看更多:https://jsfiddle.net/g1d5f7mt/8/

答案 6 :(得分:2)

试试这个:

var time = new Date().toJSON();

答案 7 :(得分:2)

一个行代码。

var date = new Date(new Date().getTime());

var date = new Date(1584120305684);

答案 8 :(得分:0)

使用datejs

new Date().toString('yyyy-MM-d-h-mm-ss');

答案 9 :(得分:0)

/Date(1383066000000)/

function convertDate(data) {
    var getdate = parseInt(data.replace("/Date(", "").replace(")/", ""));
    var ConvDate= new Date(getdate);
    return ConvDate.getDate() + "/" + ConvDate.getMonth() + "/" + ConvDate.getFullYear();
}

答案 10 :(得分:0)

假设日期为毫秒日期为1526813885836,因此您可以使用此示例代码以字符串形式访问日期:

console.log(new Date(1526813885836).toString());

清楚见下面的代码:

const theTime = new Date(1526813885836);
console.log(theTime.toString());