This blog article shows you a code sample on how to extract the date number from the date data type. I was testing this using GitHub Copilot. As you can see from the following screenshot. I prompt the Copilot to “Write a function to extract 2 digits for day, 2 digits for month and 2 digits for year form a date”.
In came out quite a decent generated code but … no function.
Also: Generate Code using GitHub Copilot, Gemini, ChatGPT, and Bing Chat
I have wrapped it with a function like the following.
public static void ExtractDate()
{
DateTime date = DateTime.Now;
// Extracting 2 digits for day, month, and year
string day = date.ToString(“dd”);
string month = date.ToString(“MM”);
string year = date.ToString(“yy”);
// Printing the extracted values
Console.WriteLine($”Day: {day}”);
Console.WriteLine($”Month: {month}”);
Console.WriteLine($”Year: {year}”);
}
Source code download: https://github.com/chanmmn/general/tree/master/2024/GetDate2Digit/?WT.mc_id=DP-MVP-36769
Reference: https://code.visualstudio.com/docs/copilot/overview/?WT.mc_id=DP-MVP-36769
https://learn.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/tutorials/?WT.mc_id=DP-MVP-36769