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);
Related
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");
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>
This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 5 years ago.
I'm writing an automation date and I want to extract a string, turn it into a DateTime object, and compare it with the current date and time.
The string in question has this format: 7/28/2017 1:17:29 PM
How can I convert it to a DateTime object to compare with the current time (Basically, my end goal is to verify that it is within a few minutes of the current time)
Use DateTime.Parse() or DateTime.TryParse()
System.Convert can convert to and from many types. For example...
int intElapsedMinutes = (DateTime.Now - Convert.ToDateTime("7/28/2017 1:17:29 PM")).TotalMinutes
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.
This question already has answers here:
How does DateTime.ToUniversalTime() work?
(4 answers)
Closed 8 years ago.
In my code, I am getting Date value as string (reading from xml doc) where no time zone is specified. Here is the sample code...
string dateStr = "2012-06-23";
DateTime convertedDate = DateTime.Parse(dateStr).ToUniversalTime();
When I check the value of convertedDate, it is "22/06/2012 18:30:00". I want to know how exectly ToUniversalTime() method works in this case and how it concluded dateTime format in this case.
Thanks in advance.
The Parse method sets the Kind property of the date to DateTimeKind.Unspecified as there is no time zone information in the string. The ToUniversalTime method assumes that the time is local and converts it to UTC.
Ref: DateTime.Parse Method
"Generally, the Parse method returns a DateTime object whose Kind
property is DateTimeKind.Unspecified."
Ref: DateTime.ToUniversalTime Method
"Unspecified: The current DateTime object is assumed to be a local time, and the
conversion is performed as if Kind were Local."