比这段代码更好? [英] better than this code?
本文介绍了比这段代码更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好..
此函数将String转换为Date,输入的字符串格式为"yyyy-MMM-dd hh:mm:ss",返回格式为dd-mm-yyyy
它工作得很好..
我的问题是:此代码是否有任何改进?
hello everyone..
This function to convert String to Date, the input''s string format is "yyyy-MMM-dd hh:mm:ss" and the return format dd-mm-yyyy
It is working perfectly..
My question is: is there any improvements for this code?
Private Function ConvertToDate(ByVal d As String) As DateTime
d = d.Replace(" 00:00:00", "")
d = d.Replace("/", "")
d = d.Replace("\", "")
d = d.Replace("-", "")
If (((d.ToLower.Contains("a") Or d.ToLower.Contains("e")) Or d.ToLower.Contains("u")) Or d.ToLower.Contains("o")) Then
If d.Contains("Jan") Then
d = d.Replace("Jan", "01")
End If
If d.Contains("Fep") Then
d = d.Replace("Fep", "02")
End If
If d.Contains("Mar") Then
d = d.Replace("Mar", "03")
End If
If d.Contains("Apr") Then
d = d.Replace("Apr", "04")
End If
If d.Contains("May") Then
d = d.Replace("May", "05")
End If
If d.Contains("Jun") Then
d = d.Replace("Jun", "06")
End If
If d.Contains("Jul") Then
d = d.Replace("Jul", "07")
End If
If d.Contains("Aug") Then
d = d.Replace("Aug", "08")
End If
If d.Contains("Sep") Then
d = d.Replace("Sep", "09")
End If
If d.Contains("Oct") Then
d = d.Replace("Oct", "10")
End If
If d.Contains("Nov") Then
d = d.Replace("Nov", "11")
End If
If d.Contains("Dec") Then
d = d.Replace("Dec", "12")
End If
d = d.Insert(d.Length, (Conversions.ToString(d.Chars(2)) & Conversions.ToString(d.Chars(3))))
d = d.Insert(d.Length, (Conversions.ToString(d.Chars(0)) & Conversions.ToString(d.Chars(1))))
d = d.Remove(0, 4)
End If
If (d = "") Then
Return New DateTime(1, 1, 1)
End If
Dim str3 As String = d.Remove(4)
Dim str2 As String = d.Remove(0, 4).Remove(2)
Dim str As String = d.Remove(0, 6)
Return New DateTime(Conversions.ToInteger(str3), Conversions.ToInteger(str2), Conversions.ToInteger(str))
End Function
从答案中移出
好吧,您的所有代码都更好,并且给出了相同的结果..
但是,如果输入是"yyyymmdd",那么上一个函数可以做到这一点,如何用您的代码将其转换?
谢谢大家:)
Moved from Answer
Well, all of your codes is better and gave the same result..
but what if the input was "yyyymmdd", the previous function can do that, how to convert it with your codes?
Thank you all :)
推荐答案
类似的方法可能对您有用;
Something like this might work for you;
Private Function ConvertToDate(ByVal d As String) As DateTime
Dim result As DateTime
If DateTime.TryParse(d, result) Then
Return result.Date
Else
Throw New ArgumentException("not a valid date", "d")
End If
End Function
希望这会有所帮助,
弗雷德里克(Fredrik)
Hope this helps,
Fredrik
string strinput = "2011-Feb-09 04:30:30"; //yyyy-MMM-dd hh:mm:ss
DateTime dt = Convert.ToDateTime(strinput, System.Globalization.CultureInfo.InvariantCulture);
Response.Write(dt.ToString("dd-MM-yyyy"));
Does this help?
实际上会返回一个DateTime
对象已初始化,其中包含已分析字符串的年,月和日.
我想您可以通过
获得相同的结果
Actually it returns aDateTime
object initialized with year, month and day of parsed string.
I guess you may obtain the same result with
Private Function ConvertToDate(ByVal d As String) As DateTime
Dim dt As DateTime = DateTime.Parse(d)
Return New DateTime(dt.Year, dt.Month, dt.Day)
End Function
这篇关于比这段代码更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文