将时间戳转换为 yyyy-mm-dd hh:mm:ss 格式,使用 JavaScript。

huangapple go评论78阅读模式
英文:

Convert timestamp to yyyy-mm-dd hh:mm:ss format using javascript

问题

需要将当前时间戳(例如:1578293326452)转换为yyyy-mm-dd hh:mm:ss格式,可以使用JavaScript。我获取当前时间戳的方法如下:

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

您要如何更改格式?

英文:

I need to convert the current timestamp (Eg: 1578293326452) to yyyy-mm-dd hh:mm:ss format
using javascript.
I obtained the current timestamp as follows:

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

How can I change the format?

答案1

得分: 1

以下是您提供的代码的翻译部分:

function getTime(){
  var date = new Date();
  
  var year = date.getFullYear();
  var month = (date.getMonth() +1);
  var day = date.getDate();
  
  var hour = date.getHours();
  var minute = date.getMinutes();
  var second = date.getSeconds();
  
  return formateTime(year, month, day, hour, minute, second);
}

function formateTime(year, month, day, hour, minute, second){
  return makeDoubleDigit(year) + "-" + 
         makeDoubleDigit(month) + "-" + 
         makeDoubleDigit(day) + " " + 
         makeDoubleDigit(hour) + ":" + 
         makeDoubleDigit(minute) + ":" + 
         makeDoubleDigit(second);
}

function makeDoubleDigit(x){
  return (x < 10) ? "0" + x : x;
}

console.log(getTime())

请注意,我已经将代码中的HTML标签(<-- begin snippet --><-- end snippet -->)移除,以便提供代码的清晰翻译部分。如果您需要进一步的帮助,请随时告诉我。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function getTime(){
  var date = new Date();
  
  var year = date.getFullYear();
  var month = (date.getMonth() +1);
  var day = date.getDate();
  
  var hour = date.getHours();
  var minute = date.getMinutes();
  var second = date.getSeconds();
  
  return formateTime(year, month, day, hour, minute, second);
}

function formateTime(year, month, day, hour, minute, second){
  return makeDoubleDigit(year) + &quot;-&quot; + 
         makeDoubleDigit(month) + &quot;-&quot; + 
         makeDoubleDigit(day) + &quot; &quot; + 
         makeDoubleDigit(hour) + &quot;:&quot; + 
         makeDoubleDigit(minute) + &quot;:&quot; + 
         makeDoubleDigit(second);
}

function makeDoubleDigit(x){
  return (x &lt; 10) ? &quot;0&quot; + x : x;
}

console.log(getTime())

<!-- end snippet -->

答案2

得分: -1

也许这是你需要的

d = Date.now();
d = new Date(d);
d = (d.getMonth()+1)+'/'+d.getDate()+'/'+d.getFullYear()+' '+(d.getHours() > 12 ? d.getHours() - 12 : d.getHours())+':'+d.getMinutes()+' '+(d.getHours() >= 12 ? "PM" : "AM");

console.log(d);
英文:

Maybe this is what you need

d = Date.now();
d = new Date(d);
d = (d.getMonth()+1)+&#39;/&#39;+d.getDate()+&#39;/&#39;+d.getFullYear()+&#39; &#39;+(d.getHours() &gt; 12 ? d.getHours() - 12 : d.getHours())+&#39;:&#39;+d.getMinutes()+&#39; &#39;+(d.getHours() &gt;= 12 ? &quot;PM&quot; : &quot;AM&quot;);

console.log(d);

huangapple
  • 本文由 发表于 2020年1月6日 15:40:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定