Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
i Have a label include a String (Example : Date) Label1.text= 2011/12/24

Now i want cut Year (4 Character) and insert into another Label (Example : label2=2011)

Then Cut month and insert into label3(Label3 = 12)

And Finally Cut Day and insert it to Label4 (label4 = 24)

Thank you -
Posted

You have a problem of code design. Don't cut anything. Get some date using System.DateTime. Suppose this should be the same data for all labels.

Now—
synctor wrote:
I have a label include a string (example: Label1.Text = "2011/12/24"). [Spelling and code fixed — SA]"


Aha. Stop having it. :-)

Now, create different strings for different labels using the same instance of data but different formats. Use System.DateTime.ToString(string customFormat) using custom format strings: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

Assign the results to the properties Text of different labels. That's it.

—SA
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 16-Nov-11 4:24am    
A vote of 1? What is that because some authors of other "solutions" was not happy with my votes? At least I spent time to explain why I down-voted them...
--SA
Prerak Patel 16-Nov-11 5:54am    
It wasn't me atleast. But I think this can be easily done with string functions, then we don't require to convert it to date and get a year/month/day out of it.
a best way to do is as follows

C#
DateTime dt = Convert.ToDateTime(lblDate.Text);
lblYear.Text = dt.Year;
lblMonth.Text = dt.Month;
lblDate.Text = dt.Date;


or you can do by finding in string too, but its not a good idea.. still I am showing it here..

C#
lblYear.Text = lblDateTime.Text.Substring(0,4);
lblMonth.Text = lblDateTime.Text.Substring(5,2);
lblDate.Text = lblDateTime.Text.Substring(8,2);


mark as answer if solves your problem, it motivates :)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Nov-11 4:17am    
Pandya, I'm really sorry, but your solution is not much better that one by Prerak which you criticized.

Why would you even consider use Substring? This is not just maintainable. It's good that you said "not a good idea", but it would be the best not to show such code, just for aesthetic reasons...

The first solution is not so good because you use implicit conversion (only good in VB.NET, not C#). This is uncontrolled way to format text. Convert is not nice, because System.DateTime does have appropriate constructor. (The word Convert is not correct, and this class is often abused.)

Why would you need all that if System.DateTime.ToString(string) does all the custom formatting. This is the only neat way. Overall, I decided to vote 3, sorry.

--SA
Pandya Anil 16-Nov-11 4:59am    
thanks for your time for voting and for pointing me my wrongs..
but I think if you do only one conversion from string to DataTime is good idea v/s you do 3 substring operations do find your result, in future if want day/month/year in any other format you can get it easily. thanks once again..
have a look at the following example :
VB
Module Module1

    Sub Main()
    ' The file system path we need to split
    Dim s As String = "C:\Users\Sam\Documents\Perls\Main"

    ' Split the string on the backslash character
    Dim parts As String() = s.Split(New Char() {"\"c})

    ' Loop through result strings with For Each
    Dim part As String
    For Each part In parts
        Console.WriteLine(part)
    Next
    End Sub

End Module

Output

C:
Users
Sam
Documents
Perls
Main
 
Share this answer
 
Comments
synctor 16-Nov-11 4:07am    
Thank you
member60 16-Nov-11 4:13am    
May i know the reason who and why downvoted ?
Sergey Alexandrovich Kryukov 16-Nov-11 4:21am    
Yes, sure, I did it. Please see all my post on this page including comments and you will understand it. Also, the path shown in your code is irrelevant. String manipulation is a bad idea in principle, because it is not based on data integrity. Such solutions are not maintainable, even though this solution is better than others on this page. System.DateTime should be used.

Sorry.
--SA
member60 16-Nov-11 4:31am    
in the question it is mentioned as string and not date time specifically
VB
Label2.Text =  Label1.Text.Substring(0, 4)
Label3.Text =  Label1.Text.Substring(5, 2)
Label4.Text =  Label1.Text.Substring(8, 2)
 
Share this answer
 
v2
Comments
synctor 16-Nov-11 4:04am    
thank you
Pandya Anil 16-Nov-11 4:08am    
you could have guided him to use DateTime.
Sergey Alexandrovich Kryukov 16-Nov-11 4:09am    
You are right. I explained it in full, please see my solution.
(Sorry, Prerak, the real solution is easy, this one is not just maintainable.)
--SA
Prerak Patel 16-Nov-11 5:55am    
I agree, but I think converting to date and get the year/month out of it is too much for such simple thing. So, I proposed it. I was to ignore this question actually.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900