This question already has answers here:
Format datetime in asp.net mvc 4
(3 answers)
Closed 3 years ago.
I understand that if you want to parse a dateTime String in a specific format when converting it to a DateTime object you do this
DateTime someDateTime = DateTime.ParseExact(myDateTime , "dd MMM yyyy HH:mm:ss", CultureInfo.InvariantCulture);
However when using a model binding with MVC C# the code is declared like this
public DateTime someDateTime {get; set;}
When doing this how do you set the format which incoming date string are expected to have?
You can have a separate property for having the formatted version of the date:
public DateTime someDateTime { get; set; }
public DateTime someDateTimeFormatted {
get {
DateTime.ParseExact(someDateTime, "dd MMM yyyy HH:mm:ss", CultureInfo.InvariantCulture);
}
}
If the date you're trying to serialize will not naturally serialize into a datetime you can write a custom serialization for it of you can pass it in the request as a string and parse it in a getter of a diff model property similar to what someDateTimeFormatted is doing above
Related
I'm working with a DateOnly variable and I'm trying to get the DateTime.Now time in a dd/mm/yyyy format, however it's only returning the date on mm/dd/yyyy format.
I strictly need the current date in a dd/mm/yyyy format, and I haven't been able to figure it out how to.
This is an example how I'm working to convert the DateTime.Now to DateOnly type
public class Example
{
public DateOnly? Date{get; set;}
}
public class Process1
{
Example example = new Example();
{
example.Date= DateOnly.FromDateTime(DateTime.Now);
//this is returning the current date in a mm/dd/yyyy format
}
}
Formatting can only be done by string not by date only.
save date in dateonly datatype
example.Date= DateOnly.FromDateTime(DateTime.Now);
but when you need specify format then use string like below
string s = example.Date.ToString("dd/M/yyyy", CultureInfo.InvariantCulture);
or
s = example.Date.ToString("dd/MM/yyyy");
For More detail refer this Link
I set the Date variable as a DateTime property in the Example Class :
public DateTime Date { get; set; } = DateTime.Now;
In the main code, i converted the Date property into a string and assigned it to dateOnly Variable:
string dateOnly = Convert.ToString(example.Date.ToString("dd-mm-yyyy"));
when i try to convert string to date following exception occured
FormatException: String '11/17/2020 3:23 PM' was not recognized as a valid DateTime.
my code is ...
public class PaperDTO
{
...
[DeadLineValidate(maxday: 62, ErrorMessage = "please specify valid date maximum 62 days are valid")]
public string DeadLine { get; set; }
...
}
public class DeadLineValidate : ValidationAttribute
{
public DeadLineValidate(int maxday){ Maxday = maxday; }
public int Maxday { get; }
public override bool IsValid(object value)
{
var date = DateTime.ParseExact(value as string, "MM/dd/yyyy hh:mm tt", CultureInfo.InvariantCulture);
return DateTime.Today <= date && date <= DateTime.Today.AddDays(Maxday);
}
}
if i use in my action method...
Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
then it works fine
but in this approach i have to change culture of thread just for date conversion and unnecessarily i have to write extra code in action method of controller
does anyone have any idea regarding this ?
Your date pattern requires 2 digits for the hour, but your value has only 1 digit. Try this date pattern instead, it will support both 1 and 2 digits:
"MM/dd/yyyy h:mm tt"
Note that the same issue (and the same solution) might apply for the Month and Day values.
I would like serialize object to XML. This part is easy.
But i would serialize properties (like DateTime) with a specific format.
My current code is this one :
[XmlIgnore]
private DateTime _paymentDate { get; set; }
public string PaymentDate
{
get
{
return this._paymentDate.ToString("yyyy-MM-dd HH:mm:ss");
}
set
{
this._paymentDate = DateTime.ParseExact(value, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
}
}
But it's very bad. If i have 100 DateTime properties, i should duplicate this code 100 times.
So, i would like to use a formatter (like with Newtonsoft JSON).
How can i do it ?
Thanks a lot :)
I am working with the you-tube API, which returns JSON data. The video published date is in this format: "publishedAt": "2017-04-30T18:18:41.000Z".
After deserializing the JSON object, I want to get the date from the published DateTime in C#.
How can I do it, and what is this format of DateTime?
There's absolutely no need to manually parse a well-formatted ISO 8601 date.
Simply change the property on your model from string to DateTime:
public class VideoData
{
[JsonProperty("publishedAt")]
public DateTime PublishedAt { get; set; }
}
And then deserialize into that:
var model = JsonConvert.DeserializeObject<VideoData>(jsonString);
And Json.NET will handle the rest.
I have json where date is defined like this;
{\"$date\":\"1947-11-13T00:00:00.000Z\"}
and my deserilising code look like this:
return JsonConvert.DeserializeObject<U>(szResp,
new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd"
});
The object I'm creating is just
[JsonProperty(PropertyName = "$date")]
public string date { get; set; }
but instead of 13/11/1947 00:00:00
I'm receiving : 11/13/1947 00:00:00
I have no clues why this deserilizer behaves like this and changes data format.
More over if I try to see data in Json visualizer in visual studio my date is shown properly "13/11/1947 12:00:00 πμ"