Displaying a day format from a datetime query [duplicate] - c#

This question already has answers here:
Is there an easy way to create ordinals in C#?
(23 answers)
Closed 3 years ago.
Not sure what the terminology is so been unable to search the problem. I am wishing to extract the day from a datetime query (SQL), asp.net, c# etc and then display as in the following examples.
Date: 21/01/2019(UK) or 01/21/2019(US) display as 21st instead of 21
Date: 25/02/2018(UK) or 02/25/2018(US) display as 25th.
I have no problem extracting the day, I just need to know if it is possible to use the syntax as shown? Or what this syntax is even called?

You can use momentjs format date.format('Do')
let dateStr = '21/01/2019';
let date = moment(dateStr, 'DD/MM/YYYY');
console.log(date.format('Do'));
dateStr = '02/25/2018';
date = moment(dateStr, 'MM/DD/YYYY');
console.log(date.format('Do'));
let dateStr = '21/01/2019';
let date = moment(dateStr, 'DD/MM/YYYY');
console.log(date.format('Do'));
dateStr = '02/25/2018';
date = moment(dateStr, 'MM/DD/YYYY');
console.log(date.format('Do'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>

Related

Get time ticks in DateTime format C# [duplicate]

This question already has answers here:
Converting the date time to display milliseconds in C#
(3 answers)
Closed 1 year ago.
Date time comes from Database side like this "2021-03-08T21:27:21.065" and then i have tried and format it from C# side like below,
string fullDate = Convert.ToDateTime(x.start_date).ToString("dd-MM-yyyy HH:mm:ss");
But i need to show last part of the date 065 as well.
"dd-MM-yyyy HH:mm:ss.fff" is the format to include milliseconds. Please try this:
string fullDate = Convert.ToDateTime(x.start_date).ToString("dd-MM-yyyy HH:mm:ss.fff");

Check if string is a valid date, and if it is, whether it is in a specific range [duplicate]

This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 5 years ago.
So i'm trying to validate date in format DDMMYYYY.
string date = "12031996";
i want to be able to validate if what user entered is in this specific format. And also want to be able to check if the year is in range of 1980 - 2005.
I'm new to C# so please forgive if it sounds like a stupid question.
Thanks :)
if (DateTime.TryParseExact(date, "ddMMyyyy", NULL, DateTimeStyle.None, out DateTime dateTime))
{
int year = dateTime.Year;
if (year >= 1980 && year <= 2005)
//do something if in range
else
//do something otherwise
}
else
//do something for invalid format.
To learn more about how to parse a date time string in C#, read this page on MSDN and this one.

add the specified number of days to current date in c# [duplicate]

This question already has answers here:
Add 1 week to current date
(3 answers)
Closed 8 years ago.
I want to add number of days with the current date. Help me to find a proper solution. Thank you.
Code:
string s = DateTime.Now.ToString();
I want to add 2 days with the current date.
string s = DateTime.Now.AddDays(2).ToString();
UPDATE
In answer to your comment
string s = DateTime.Now.AddDays(2).ToShortDateString();
DateTime.Now.AddDays(2).ToString();
You can use the AddDays method on the DateTime struct. You can use that method to add or substract any amount of days from the current date, like in this sample.
DateTime added = DateTime.Now.AddDays(2);
string s = added.ToString();
Or even:
DateTime substracted = DateTime.Now.AddDays(-2);
Note that if you are only interested in the date, you could use DateTime.Today rather than DateTime.Now since that will be a little more performant.
A final note on the use of ToString: the output of ToString may differ when the OS running on uses different cultures. If you intend to process this outputted string later on, I suggest to pass in the desired culture, using this overload of ToString.

Subtract one day from DateTime object [duplicate]

This question already has answers here:
Is there any easy way to increment a DateTime by monthly/yearly/daily units without having to parse it out like crazy?
(4 answers)
Closed 7 years ago.
I want to substrate one day from my date, so that when my date is 30-7-2013 than I want 29-7-2013
Just use the AddDays method, remembering that it doesn't change the value it's called on - it returns a new DateTime value.
DateTime date = ...;
date = date.AddDays(-1);
I am not familiar with windows phone development but you may try to use DateTime.AddDays(-1) method call. According to MSDN this call is supported by windows phone OS.
For example:
var today = DateTime.Now;
var yesterday = today.AddDays(-1);
Alternative version:
DateTime date = ...;
date -= TimeSpan.FromDays(1);

Convert string to data in form of dd/mm/yyyy [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
convert string in a text box to dd/mm/yyyy date
When I try to parse
DateTime.ParseExact("22/11/2012", "dd/mm/yyyy", CultureInfo.InvariantCulture).ToString();
I got
1/22/2012 12:11:00 AM
I want to return back to exactly the same origin date which is 22/11/2012.
use "dd/MM/yyyy" mm - minutes, MM - month
check patterns here.
The month is actually referenced with 'MM'. Documentation

Categories