简体   繁体   English

将奇怪的日期格式转换为短DateTime

[英]Convert weird date format to short DateTime

I have a date string, returne from a ExtJS datetime picker, wich looks like this : 我有一个日期字符串,从ExtJS 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. 由此,我需要使用C#或JavaScript格式:YYYY-mm-dd。 How could I do this ? 我该怎么办? I've tried using DateTime.Parse and it cannot be parsed. 我尝试使用DateTime.Parse,但无法解析。 Any idea? 任何想法?

Thank you! 谢谢!

You don't seem to care about the time and timezone information so you can in fact use DateTime.ParseExact to parse the date. 您似乎并不在乎时间和时区信息,因此实际上您可以使用DateTime.ParseExact来解析日期。 Assuming that the day of month part may be just a single digit (eg 2012-04-01 is Sun Apr 1 2012 ) the pattern you need to use is ddd MMM d yyyy . 假设月份中的一天部分可能只是个数字(例如2012-04-01是Sun Apr 1 2012 ,星期日),则需要使用的模式是ddd MMM d yyyy

The "hardest" part is really chopping of the time and timezone part. “最困难的”部分实际上是时间和时区部分的斩波。 If the day of month is a single digit you have to take of substring of length 14; 如果一个月中的某天是一位数字,则必须采用长度为14的子字符串; otherwise of length 15. Here is a way to get the index of the 4th space character in a string: 否则为长度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);

You can then parse the date into a DateTime : 然后,您可以将日期解析为DateTime

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

This can be formatted back into a string using whatever format and culture you need. 可以使用所需的任何格式和区域性将其格式化为字符串。

In .NET, where you have a string representation of a date that has a guaranteed format, you can use DateTime.ParseExact to parse it: 在.NET中,如果日期的字符串表示形式具有保证的格式,则可以使用DateTime.ParseExact进行解析:

var input  = "Wed Apr 25 2012 00:00:00 GMT+0300 (GTB Daylight Time)"
                            .Substring(0, 15);
var format = "ddd MMM dd YYYY";
var date = DateTime.ParseExact(input, format, CultureInfo.InvariantCulture);

// Now date is a DateTime holding the date

var output = date.ToString("yyyy-MM-dd");

// Now output is 2012-04-25

也许这可以帮助您单击

In JavaScript 在JavaScript中

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

Will give you a date object. 将给您一个日期对象。 You can then format it to your date format (or preferably ISO8601, or milliseconds from the epoc using .getTime()) by picking out the (UTC) year, month and day 然后,您可以通过选择(UTC)年,月和日,将其格式化为日期格式(最好是ISO8601,或者使用.getTime()从epoc到毫秒)。

try this using Javascript. 尝试使用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;

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

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