English 中文(简体)
更改日期格式
原标题:Convert weird date format to short DateTime

我有一天的路,从外国航天公司时代的客座中返回。

Wed Apr 25 2012 00:00:00 GMT+0300 (GTB Daylight Time)

From this I would need to have it in this format : YYYY-mm-dd, using C# or JavaScript. How could I do this? I ve tried using DateTime.Parse and it cannot be parsed. Any idea?

最佳回答

你似乎不关心时间和时间区信息,因此你实际上可以使用<代码>。 日期:ParseExact 假设月份部分日仅限一位数(例如,2012-04-01为<代码>Sun Apr 1 2012) 您需要使用的模式如下:ddd MMM d yyyyy

“最硬”部分实际上占用了时间和时间区部分。 如果月份为一位数位数,你必须补足14年;否则时间为15年。 这里是将第4号空间特性指数推向如下方向的一个途径:

var str = "Wed Apr 25 2012 00:00:00 GMT+0300 (GTB Daylight Time)";
var index = -1;
for (var i = 0; i < 4; i += 1)
  index = str.IndexOf(   , index + 1);

然后,你可将日期分成<代码>。 日期:

var date = DateTime
  .ParseExact(str.Substring(0, index), "ddd MMM d yyyy", CultureInfo.InvariantCulture);

这可以采用你所需要的任何形式和文化的形式,重新形成。

问题回答

利用Javascript进行审判。

var d = new Date( Wed Apr 25 2012 00:00:00 GMT+0300 (GTB Daylight Time) );
var curr_day = d.getDate();
var curr_month = ( 0 +(d.getMonth()+1)).slice(-2)
var curr_year = d.getFullYear();
var new_date  = curr_year+"-"+curr_month+"-"+curr_day;

In JavaScript

new Date("Wed Apr 25 2012 00:00:00 GMT+0300 (GTB Daylight Time)")

将给你一个日期反对。 届时,你可以通过抽出(UTC)年、月和日的形式(或最好是ISO8601,或用植被流出的流体)将其格式。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!