Convert DateTime.Now to DateOnly in dd/mm/yyyy - c#

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"));

Related

Parsing date time strings with C# MVC [duplicate]

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

Get date from ISO8601 format in C#

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.

using Json ATTRIBUTE to serialize datetime to look like /Date(1486516302715) in C#

class className
{
Datetime dateTime {get; set;}
}
className object = new className();
When I use Json(object) this returns the dateTime field in this format :
{"dateTime":"/Date(1486516302715)/"}
But what I want is to return the same value using an attribute to the dateTime parameter instead.
Something like :
[JsonConverter(typeof(JavaScriptDateTimeConverter))]
Datetime dateTime {get; set;}
Only the JavaScriptDateTimeConverter is not the one which gives me the date in the right format.
In javascript you could use the new Date method.
new Date(parseInt(date.substr(6))); // date= "/Date(1486516302715)/"

Json converter change date format while deserializing

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 πμ"

Convert a get;set string DateEntered from yyyy-MM-dd to MM-dd-yyyy in C#?

In one .cs file I have the following:
public string DateEntered { get; set; }
In another .cs file then I have another string to grab a certain date.
public string HighestDate
{
get
{
...
return d.DateEntered;
}
}
Wondering how to convert the output from yyyy-MM-dd to MM-dd-yyyy? The scope in which it is used is as follows:
data.HighestDate
Thanks in advance.
EDIT:
Another part of the code that I think is integral:
var s = this...(i => i.DateEntered == DateTime.UtcNow.ToString("MM-dd-yyyy"));
However this only changes certain instances of the date format.
You should always store dates as DateTime variables:
public DateTime DateEntered { get; set; }
Then you can render the date however you like in your consumer application (UI):
public string HighestDate
{
get
{
...
return d.DateEntered.ToString("MM-dd-yyyy");
}
}
If you save them both as DateTime, then you can use data.HighestDate.ToString("MM-dd-yyyy"). Otherwise, you could use:
DateTime.ParseExact(data.HighestDate, "yyyy-MM-dd", CultureInfo.InvariantCulture).ToString("MM-dd-yyyy")
Although you should still try to modify your code to save them as DateTime objects, this is the right-proper way to do it. (And saves memory space.)
https://msdn.microsoft.com/en-us/library/w2sa9yss%28v=vs.110%29.aspx
https://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx
Quick Googling would have net you your result.
Firstly, I recommend using a DateTime to store the value and turn it into a string at the last possible moment.
Secondly, use DateTime.ParseExact to read a particular format from a string and use ToString("some format") to format a DateTime to a string
DateTime parsedDate;
if (DateTime.TryParse(data.HighestDate, out parsedDate))
{
var reformatted = parsedDate.ToString("MM-dd-yyyy");
}

Categories

Resources