ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,178 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi.
I have a program in MVC.
It gets data from AS400 such as birthday and display it but in decimal like 06051985. I want it to be displayed in 06/05/1985.
How will I do it and where (View, Model, Controller?).
Thanks.
You can do something like below by having a GETTER only property in your Model.
MyModel myModel = new MyModel { BirthdayAsInt = 06051985 };
Console.WriteLine(myModel.Birthday); // 06/05/1985
public class MyModel
{
public int BirthdayAsInt { get; set; }
public string Birthday
{
get
{
// TODO: Validate BirthdayAsInt is in the expected format, otherwise below can throw can error
// Or make the following code defensive
string birthdayAsString = BirthdayAsInt.ToString("D8");
int date = Convert.ToInt32(birthdayAsString.Substring(0, 2));
int month = Convert.ToInt32(birthdayAsString.Substring(2, 2));
int year = Convert.ToInt32(birthdayAsString.Substring(4, 4));
return new DateTime(year, month, date).ToString("dd/MM/yyyy");
}
}
}