简体   繁体   English

如何在JavaScript中转换dateTime格式

[英]How to convert dateTime format in javascript

我如何将datetime 5/8/2011 12:00:00 AM (m / d / yyyy)转换为dd-MMM-yyyy,如JavaScript中08-May-201108-May-2011

This link is a good resource you can use for. 该链接是您可以使用的很好的资源。

http://blog.stevenlevithan.com/archives/date-time-format http://blog.stevenlevithan.com/archives/date-time-format

Alternatively, you need to get the individual part and concatenate them as needed like below. 另外,您需要获取单个部分并根据需要将它们连接起来,如下所示。

var now = new Date();
var hour        = now.getHours();
var minute      = now.getMinutes();
var second      = now.getSeconds();
var monthnumber = now.getMonth();
var monthday    = now.getDate();
var year        = now.getYear();

String myOutput = monthday + "-" + monthnumer + "-" + year;

To get the month name instead of month number, you need to define an array like below 要获取月份名称而不是月份编号,您需要定义如下数组

var arrMonths = new Array ("Jan","Feb"....};

   String myOutput = monthday + "-" + arrMonths[monthnumer-1] + "-" + year;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM