Hi
In a Date Parameter is it possible that we can specify Date Format so User can enter date in dd/MM/yyyy format
Thanks
Hi
In a Date Parameter is it possible that we can specify Date Format so User can enter date in dd/MM/yyyy format
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Jan 5, 2025, 8:17 AM EST
Hi Ramco,
Yes, you can specify a date format for a parameter as per your requirment. If you are working on windows form you can specify as below:
dateTimePicker1.Format = DateTimePickerFormat.Custom; dateTimePicker1.CustomFormat = "dd/MM/yyyy";For MVC, you can specify a custom DateFormat attribute to control how the date is displayed and parsed.
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] public DateTime MyDate { get; set; }Jaish MathewsRank #225 · 11K reputationPosted Jan 5, 2025, 11:05 AM EST
In C#, you can enforce a specific date format, such as
dd/MM/yyyy, when accepting date input from users. However, this depends on the context in which the date input is handled (e.g., console application, web application, or desktop application). Here's how you can achieve it:1. Console Application
You can parse user input into a
DateTimeobject using a specific format:using System; using System.Globalization; class Program { static void Main() { Console.WriteLine("Enter a date in dd/MM/yyyy format:"); string input = Console.ReadLine(); if (DateTime.TryParseExact(input, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime parsedDate)) { Console.WriteLine($"You entered: {parsedDate.ToString("dd/MM/yyyy")}"); } else { Console.WriteLine("Invalid date format. Please enter the date in dd/MM/yyyy format."); } } }This uses
DateTime.TryParseExactto validate and parse the input.2. Web Application (ASP.NET/Blazor)
a) ASP.NET MVC or Razor Pages
You can enforce a date format using the
[DataType]or[DisplayFormat]attributes:public class MyModel { [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] [DataType(DataType.Date)] public DateTime MyDate { get; set; } }In the view, ensure you use a date input that supports the format:
b) Blazor
In Blazor Server, use an
InputTextwith custom parsing logic to enforce the format:@Message @code { private string DateInput { get; set; } private string Message { get; set; } private void ValidateDateFormat() { if (DateTime.TryParseExact(DateInput, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out DateTime date)) { Message = $"Valid date: {date.ToString("dd/MM/yyyy")}"; } else { Message = "Invalid date. Please use the dd/MM/yyyy format."; } } }3. Windows Forms or WPF
For desktop applications, you can use a
MaskedTextBoxto enforce a format likedd/MM/yyyy:Windows Forms:
MaskedTextBox maskedTextBox = new MaskedTextBox("00/00/0000"); // Mask for dd/MM/yyyy maskedTextBox.ValidatingType = typeof(DateTime); maskedTextBox.TypeValidationCompleted += (sender, e) => { if (!e.IsValidInput) { MessageBox.Show("Invalid date. Please use the dd/MM/yyyy format."); } };WPF:
Use a
TextBoxwith validation logic or bind to a date picker with a custom format.Summary
While it’s possible to specify a date format like
dd/MM/yyyy, it’s essential to validate and parse the input properly. The actual implementation depends on the type of application and the control used for input.Jignesh KumarRank #91 · 22.9K reputationPosted Jan 5, 2025, 10:56 PM EST
Hello Ramco,
Best way to handle date format in crystal report is configure your date filed at application level, in your c# code you can do this one,
DateTime userInput; if (DateTime.TryParseExact(userInputString, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out userInput)) { report.SetParameterValue("YourDateParameter", userInput); } else { // Display any message if a date is not in correct format MessageBox.Show("Please enter the date in dd/MM/yyyy format."); }