如何DateTime格式转换的JavaScript [英] How to convert dateTime format in javascript

查看:135
本文介绍了如何DateTime格式转换的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能转换日期时间 2011年5月8日12:00:00 AM (M / D / YYYY),以DD-MMM-YYYY如 08 2011年5月在JavaScript。

How i could convert datetime 5/8/2011 12:00:00 AM (m/d/yyyy) to dd-MMM-yyyy like 08-May-2011 in javascript.

推荐答案

此链接,您可以使用一个很好的资源。

This link is a good resource you can use for.

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;

这篇关于如何DateTime格式转换的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆