Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
hello guys i need a code for changing the date to text in c# in winforms

What I have tried:

i searched but didn't find something beautiful
Posted
Updated 6-Feb-19 0:19am

C#
DateTime now = DateTime.Now;
Console.WriteLine(now.ToString("D"));
Will give you "28 January 2019"
Or there is:
C#
DateTime now = DateTime.Now;
Console.WriteLine("{0:D}", now);
Which will do the same thing.

What more words do you want?
 
Share this answer
 
Comments
Member 13985914 28-Jan-19 6:30am    
i have date in a textbox i.e 28-01-2019 and i want "Twenty Eight January, Two Thousand one"
OriginalGriff 28-Jan-19 6:51am    
So use DateTime.TryParse (or even TryParseExact) to convert the user entry to a DateTime value, then write a "number to words" converter (there are loads on the internet, it's a common bit of homework), and use it twice. The month you can get direct from the DateTime.

Don't use this one:
https://www.codeproject.com/Tips/103025/Converting-numbers-to-the-word-equivalent
Based on link provided by Rick Zeeland in solution #1, i've adapted code this way:

C#
void Main()
{
	DateTime[] dates = new DateTime[]
		{
			new DateTime(1981, 1, 28),
			new DateTime(1998, 2, 1),
			new DateTime(2000, 3, 31),
			new DateTime(2001, 4, 8),
			new DateTime(2016, 6, 5),
			new DateTime(1977, 8, 3),
			new DateTime(2012, 10, 12),
			new DateTime(1968, 12, 15)
		};

	foreach(DateTime dt in dates)
	{
		string dtw = DateToWords.Convert(dt);
		Console.WriteLine("{0} => '{1}'", dt.ToString("dd-MM-yyyy"), dtw);
	}
}

// Define other methods and classes here
public static class DateToWords
{
	private static CultureInfo ci = new CultureInfo("en-US");
	private static string[] unitsMap = new string[] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", 
				"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
    private static string[] tensMap = new string[] { "Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
		
	//https://stackoverflow.com/questions/2729752/converting-numbers-in-to-words-c-sharp
	private static string NumberToWords(int number)
	{
	    if (number == 0)
	        return "zero";
	
	    string words = "";
		
	    if ((number / 1000) > 0)
	    {
	        words += NumberToWords(number / 1000) + " Thousand ";
	        number %= 1000;
	    }
	
	    if ((number / 100) > 0)
	    {
	        words += NumberToWords(number / 100) + " Hundred ";
	        number %= 100;
	    }
	
	    if (number > 0)
	    {
	        if (words != "")
	            words += " ";
		
	        if (number < 20)
	            words += unitsMap[number];
	        else
	        {
	            words += tensMap[number / 10];
	            if ((number % 10) > 0)
	                words += " " + unitsMap[number % 10];
	        }
	    }
	
	    return Regex.Replace(words, @"\ {2,}", " ");
	}
		
	public static string Convert(DateTime dt)
	{
		//convert days
		string dtw = NumberToWords(dt.Day);
		
		//convert months
		dtw += " " + dt.ToString("MMMM", ci);	
		
		//convert years
		dtw += ", " + NumberToWords(dt.Year);
		
		return dtw;
	}
}


Result:
28-01-1981 => 'Twenty Eight January, One Thousand Nine Hundred Eighty One'
01-02-1998 => 'One February, One Thousand Nine Hundred Ninety Eight'
31-03-2000 => 'Thirty One March, Two Thousand '
08-04-2001 => 'Eight April, Two Thousand One'
05-06-2016 => 'Five June, Two Thousand Sixteen'
03-08-1977 => 'Three August, One Thousand Nine Hundred Seventy Seven'
12-10-2012 => 'Twelve October, Two Thousand Twelve'
15-12-1968 => 'Fifteen December, One Thousand Nine Hundred Sixty Eight'
 
Share this answer
 
v3
Comments
Maciej Los 7-Feb-19 2:53am    
I just added a tip: Date to words[^]
Member 13985914 7-Feb-19 8:16am    
your code is awesome but i need in winform i have (DOB in figures) in my textbox and want to show that DOB in words in another textbox

and also please make a change for me that it gives me '1990' as 'Nineteen Hundred ninety' and for '2000' it is good
Maciej Los 7-Feb-19 8:33am    
Try this:
CultureInfo ci = CultureInfo.CurentCulture; // or: new CultureInfo("en-US"); - enter your culture info
this.TextBox2.Text = DateToWords(DateTime.ParseExact(this.TextBox1.Text, "yyyy-MM-dd", ci); //enter correct date format!

If my answer meets your needs, please accept it as a soltion (green button) - formally to remove your question from unanswered list.
Member 13985914 7-Feb-19 8:16am    
thanks in advance
 
Share this answer
 
v2
Comments
Member 13985914 28-Jan-19 6:41am    
Bro i don't want format:
my problem is:
i have date in a textbox i.e 28-01-2019 and i want "Twenty Eight January, Two Thousand one"
Maciej Los 6-Feb-19 5:41am    
And...
What's wrong with second link? There you'll find complete example. All you need to do is to change it to your needs.
Tip: you have to pass each part of date to the method (day, month and finally year).

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