Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
The default date setting of my system is "MM/dd/yyyy" .I want to convert this in the textbox through my program to "dd/MM/yyyy" for this i have written this code which is giving error where month is greater than 12 .Plz help

What I have tried:

Dt.Mask = "00/00/0000"
Dim dtt As Date
dtt = Format(Now().ToString("dd/MM/yyyy"))

Dt.Text = dtt
Posted
Updated 18-Dec-23 3:22am
v2
Comments
Richard Deeming 13-Dec-23 11:50am    
1) Why are you trying to Format(...) a String?

2) What is the exact error message?

3) Why are you trying to override the user's preference, rather than write code which works with the current regional settings?

1 solution

Date format in my OS is: "yyyy-MM-dd" :)

And i'm using CultureInfo[^] to use different regional settings.
See:
VB.NET
Dim ci As System.Globalization.CultureInfo = New System.Globalization.CultureInfo("en-Us")

Dim dt As DateTime  = DateTime.Now()
Dim df As String = dt.ToString("dd/MM/yyyy", ci)

Console.WriteLine($"Date in custom format: '{df}'")

'displays: 
'Date in custom format: '30/12/2023'


More about standard/custom date formats:
Standard date and time format strings - .NET | Microsoft Learn[^]
Custom date and time format strings - .NET | Microsoft Learn[^]
 
Share this answer
 
v2

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