הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Monday, November 23, 2015 10:32 AM
How to elegantly convert text string YYYYMMDD to text string DDMMYYYY?
All replies (18)
Monday, November 23, 2015 11:02 AM ✅Answered
In VB.Net that would be like this.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim TheStingDate As String = "2015/11/23"
Dim d As Date = Date.ParseExact(TheStingDate, "yyyy/MM/dd", Globalization.CultureInfo.InvariantCulture)
MessageBox.Show(d.ToString("dd/MM/yyyy"))
End Sub
End Class
If you say it can`t be done then i`ll try it
Monday, November 23, 2015 11:21 AM ✅Answered
Hi Pat
Taking your OP at face value - - - here is one possible answer - - -
Public Class Form4
Private Sub Form4_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim S As String = "20151125"
S = S.Substring(6, 2) & S.Substring(4, 2) & S.Substring(0, 4)
Text = S
End Sub
End Class
Top Tip: Toothache? Cut paper towel to 2"square. Smear with olive oil. Sprinkle on Cayenne Pepper. Fold over few times to form small wad. Tuck in between wall of mouth and gum. Leave 1 - 2 hrs. You will thank me!
Monday, November 23, 2015 11:35 AM ✅Answered | 1 vote
For some strange reason there is a 3rd parameter but it does nothing. Strange enough gives C# an error on it when it is not Something
Module Module1
Sub Main()
Dim thestring = "20151123"
Dim theStringSetToDate = DateTime.ParseExact(thestring, "yyyyMMdd", Nothing)
Console.WriteLine(theStringSetToDate.ToString("ddMMyyyy"))
Console.ReadLine()
End Sub
End Module
Success
Cor
Monday, November 23, 2015 10:35 AM | 1 vote
By using a date/time value instead of a string.
Monday, November 23, 2015 10:41 AM | 1 vote
Hi,
Thank you for posting on MSDN forum,
You can use DateTime.ParseExact with an exam,
string res = "20151123";
DateTime d = DateTime.ParseExact(res, "yyyyMMdd", CultureInfo.InvariantCulture);
Console.WriteLine(d.ToString("dd/MM/yyyy"));
Refer MSDN article: http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx
Thanks,
If my reply is helpful please mark as Answer or vote as Helpful.
This post is my own opinion and does not necessarily reflect the opinion or view of Microsoft, its employees, or other MVPs.
Monday, November 23, 2015 11:42 AM | 1 vote
Without context it is difficult to know how to answer this.
Is this a string that you have no control over? Is this representing a date and if so do you know if the date is valid?
Taking your question at face value this code should do the trick.
Dim s As String = "YYYYMMDD"
s = New String(s.Reverse.ToArray)
"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein Multics - An OS ahead of its time.
Monday, November 23, 2015 11:50 AM
Without context it is difficult to know how to answer this.
Is this a string that you have no control over? Is this representing a date and if so do you know if the date is valid?
Taking your question at face value this code should do the trick.
Dim s As String = "YYYYMMDD" s = New String(s.Reverse.ToArray)
"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein Multics - An OS ahead of its time.
DB,
That would reverse all the characters in the string so that "2015/11/23" would become "32/11/5102". 8)
If you say it can`t be done then i`ll try it
Monday, November 23, 2015 12:44 PM
I was trying to make the point that there is not enough information. I'll give you that this looks like a date and my solution wouldn't be good for that. Guess I woke up on the wrong side of the bed and decided to do exactly what the OP asked, reverse the string.
"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein Multics - An OS ahead of its time.
Monday, November 23, 2015 1:15 PM
I was trying to make the point that there is not enough information. I'll give you that this looks like a date and my solution wouldn't be good for that. Guess I woke up on the wrong side of the bed and decided to do exactly what the OP asked, reverse the string.
"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein Multics - An OS ahead of its time.
I have those mornings all the time, reversing the string was the first thing that crossed my mined when i saw this question this morning too. 8)
If you say it can`t be done then i`ll try it
Monday, November 23, 2015 2:26 PM
Without context it is difficult to know how to answer this.
Is this a string that you have no control over? Is this representing a date and if so do you know if the date is valid?
Taking your question at face value this code should do the trick.
Dim s As String = "YYYYMMDD" s = New String(s.Reverse.ToArray)
"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein Multics - An OS ahead of its time.
DB,
That would reverse all the characters in the string so that "2015/11/23" would become "32/11/5102". 8)
If you say it can`t be done then i`ll try it
Actually that would reverse "YYYYMMDD" to "DDMMYYYY" which is what the OP asked for.
La vida loca
Monday, November 23, 2015 3:27 PM
Without context it is difficult to know how to answer this.
Is this a string that you have no control over? Is this representing a date and if so do you know if the date is valid?
Taking your question at face value this code should do the trick.
Dim s As String = "YYYYMMDD" s = New String(s.Reverse.ToArray)
"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein Multics - An OS ahead of its time.
DB,
That would reverse all the characters in the string so that "2015/11/23" would become "32/11/5102". 8)
If you say it can`t be done then i`ll try it
Actually that would reverse "YYYYMMDD" to "DDMMYYYY" which is what the OP asked for.
La vida loca
Yes, i guess you and DB are right after rereading the question again. I guess i just automatically thought of changing a date format because it appears to be a string format for a date. If it is just for reversing a string then DB`s code would be the answer. See, i have these mornings all the time, just like i said. 8)
If you say it can`t be done then i`ll try it
Monday, November 23, 2015 5:22 PM
Without context it is difficult to know how to answer this.
Is this a string that you have no control over? Is this representing a date and if so do you know if the date is valid?
Taking your question at face value this code should do the trick.
Dim s As String = "YYYYMMDD" s = New String(s.Reverse.ToArray)
"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein Multics - An OS ahead of its time.
DB,
That would reverse all the characters in the string so that "2015/11/23" would become "32/11/5102". 8)
If you say it can`t be done then i`ll try it
Actually that would reverse "YYYYMMDD" to "DDMMYYYY" which is what the OP asked for.
La vida loca
Yes, i guess you and DB are right after rereading the question again. I guess i just automatically thought of changing a date format because it appears to be a string format for a date. If it is just for reversing a string then DB`s code would be the answer. See, i have these mornings all the time, just like i said. 8)
If you say it can`t be done then i`ll try it
Actually I figure you others are right and the OP misrepresented their actual desire. After all who wants to swap a string "YYYYMMDD" to "DDMMYYYY" when what they probably want to do is take a time displayed as "YYYYMMDD" and swap it to display as "DDMMYYYY" as most peeps provided answers too. :)
La vida loca
Monday, November 23, 2015 5:36 PM
Hi Pat
Taking your OP at face value - - - here is one possible answer - - -
Public Class Form4 Private Sub Form4_Load(sender As Object, e As EventArgs) Handles Me.Load Dim S As String = "20151125" S = S.Substring(6, 2) & S.Substring(4, 2) & S.Substring(0, 4) Text = S End Sub End Class
Top Tip: Toothache? Cut paper towel to 2"square. Smear with olive oil. Sprinkle on Cayenne Pepper. Fold over few times to form small wad. Tuck in between wall of mouth and gum. Leave 1 - 2 hrs. You will thank me!
And the winner is...!
Obviously the date parsing works too, and I'm not sure if one is really more "elegant" than the other, but when it comes to "efficient" the simple substring solution is the fastest.
Here's a comparison of substrings to date parsing to char array copying. In a 64bit release build the substrings and array copying are a close tie with substrings winning out in the end. In debug any-cpu the substring is twice as fast as array copying. In either case the date parsing is an order of magnitude slower.
Granted, we are talking about 100ths or 1000ths of a millisecond, but still...
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sourceString As String = "20151123"
Dim resultString As String = String.Empty
Dim simpleSubstring = Function(input As String)
Return input.Substring(6, 2) & input.Substring(4, 2) & input.Substring(0, 4)
End Function
Dim dateParse = Function(input As String)
Dim dt = Date.ParseExact(input, "yyyyMMdd", Globalization.CultureInfo.CurrentCulture.DateTimeFormat)
Return dt.ToString("ddMMyyyy")
End Function
Dim charCopy = Function(input As String)
Dim source = input.ToCharArray
Dim result(input.Length - 1) As Char
Array.Copy(source, 6, result, 0, 2)
Array.Copy(source, 4, result, 2, 2)
Array.Copy(source, 0, result, 4, 4)
Return New String(result)
End Function
Dim sw As New Stopwatch
sw.Start()
For i = 1 To 1000
resultString = simpleSubstring(sourceString)
Next
sw.Stop()
ListBox1.Items.Add($"simpleSubstring = {sw.Elapsed.TotalMilliseconds / 1000:n5}ms avg.")
sw.Restart()
For i = 1 To 1000
resultString = dateParse(sourceString)
Next
sw.Stop()
ListBox1.Items.Add($"dateParse = {sw.Elapsed.TotalMilliseconds / 1000:n5}ms avg.")
sw.Restart()
For i = 1 To 1000
resultString = charCopy(sourceString)
Next
sw.Stop()
ListBox1.Items.Add($"charCopy = {sw.Elapsed.TotalMilliseconds / 1000:n5}ms avg.")
End Sub
End Class
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
Monday, November 23, 2015 5:53 PM
Hi Pat
Taking your OP at face value - - - here is one possible answer - - -
Public Class Form4 Private Sub Form4_Load(sender As Object, e As EventArgs) Handles Me.Load Dim S As String = "20151125" S = S.Substring(6, 2) & S.Substring(4, 2) & S.Substring(0, 4) Text = S End Sub End Class
Top Tip: Toothache? Cut paper towel to 2"square. Smear with olive oil. Sprinkle on Cayenne Pepper. Fold over few times to form small wad. Tuck in between wall of mouth and gum. Leave 1 - 2 hrs. You will thank me!
And the winner is...!
Obviously the date parsing works too, and I'm not sure if one is really more "elegant" than the other, but when it comes to "efficient" the simple substring solution is the fastest.
Here's a comparison of substrings to date parsing to char array copying. In a 64bit release build the substrings and array copying are a close tie with substrings winning out in the end. In debug any-cpu the substring is twice as fast as array copying. In either case the date parsing is an order of magnitude slower.
Granted, we are talking about 100ths or 1000ths of a millisecond, but still...
Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim sourceString As String = "20151123" Dim resultString As String = String.Empty Dim simpleSubstring = Function(input As String) Return input.Substring(6, 2) & input.Substring(4, 2) & input.Substring(0, 4) End Function Dim dateParse = Function(input As String) Dim dt = Date.ParseExact(input, "yyyyMMdd", Globalization.CultureInfo.CurrentCulture.DateTimeFormat) Return dt.ToString("ddMMyyyy") End Function Dim charCopy = Function(input As String) Dim source = input.ToCharArray Dim result(input.Length - 1) As Char Array.Copy(source, 6, result, 0, 2) Array.Copy(source, 4, result, 2, 2) Array.Copy(source, 0, result, 4, 4) Return New String(result) End Function Dim sw As New Stopwatch sw.Start() For i = 1 To 1000 resultString = simpleSubstring(sourceString) Next sw.Stop() ListBox1.Items.Add($"simpleSubstring = {sw.Elapsed.TotalMilliseconds / 1000:n5}ms avg.") sw.Restart() For i = 1 To 1000 resultString = dateParse(sourceString) Next sw.Stop() ListBox1.Items.Add($"dateParse = {sw.Elapsed.TotalMilliseconds / 1000:n5}ms avg.") sw.Restart() For i = 1 To 1000 resultString = charCopy(sourceString) Next sw.Stop() ListBox1.Items.Add($"charCopy = {sw.Elapsed.TotalMilliseconds / 1000:n5}ms avg.") End Sub End Class
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
You mean if dbasnett's answer is not correct I suppose.
La vida loca
Monday, November 23, 2015 7:12 PM
...
You mean if dbasnett's answer is not correct I suppose.
La vida loca
I suspect that his post was more of a commentary on writing a good question than an actual proposition of a solution. We can imply that the context is a date format by the pseudo format strings used in the question, and can assume that transposing the YYYY, MM and DD includes maintaining their individual sequences. I think the point was that we shouldn't have to make assumptions based on implications, and that it is better to be more explicit about your question.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
Monday, November 23, 2015 7:25 PM
Other two elegant solutions:
Dim a = "20151123"
Dim b = String.Concat(a(6), a(7), a(4), a(5), a(0), a(1), a(2), a(3))
' or
Dim b = New String({a(6), a(7), a(4), a(5), a(0), a(1), a(2), a(3)})
Monday, November 23, 2015 7:38 PM
Other two elegant solutions:
Dim a = "20151123"
Dim b = String.Concat(a(6), a(7), a(4), a(5), a(0), a(1), a(2), a(3))
' or
Dimb = New String({a(6), a(7), a(4), a(5), a(0), a(1), a(2), a(3)})
And we have a new winner. Seems so obvious now lol
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
Monday, November 23, 2015 8:36 PM
...
You mean if dbasnett's answer is not correct I suppose.
La vida loca
I suspect that his post was more of a commentary on writing a good question than an actual proposition of a solution. We can imply that the context is a date format by the pseudo format strings used in the question, and can assume that transposing the YYYY, MM and DD includes maintaining their individual sequences. I think the point was that we shouldn't have to make assumptions based on implications, and that it is better to be more explicit about your question.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
Exactly.
"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein Multics - An OS ahead of its time.