0

I have two parameters defined in appsettings.json of an asp.net core project.

  "TimeParam": {
    "StartTime": "00:00:00.0001",
    "EndTime": "23:59:99.999"
    "BackOffDay": 1
  },

In a function, I need to pass two parameters as startDate and endDate of data type date.

For example, for the startDate I need to be like 17/06/2020 00:00:00.0001 and the endDate to be like 17/06/2020 23:59:99.999

How can I get to set the two variable as follows taking into consideration the time that I have set in the appsettings.json?

StartDate: DateTime.Now.AddDays(BackOffDay) EndDate: DateTime.Now
3
  • var startDate = DateTime.Parse($"{DateTime.Now.AddDays(BackOffDay):dd/MM/yyyy} {StartTime}"); and similarly for endDate. Commented Jun 18, 2020 at 17:33
  • System.FormatException: String was not recognized as a valid DateTime. Commented Jun 18, 2020 at 17:36
  • Is your culture set for day/month/year? If not, you can use ParseExact() instead. Commented Jun 18, 2020 at 17:39

2 Answers 2

4

You tricked me a second there, 23:59:99.999 is no parseable time as there are only 59 seconds in a minute.

But here you go:

using System;           
using System.Globalization;
public class Program
{
    public static void Main()
    {
        var start= "00:00:00.0001";
        var end = "23:59:59.999";
        int backOffDay = 1;
        
        var startTimeSpan = TimeSpan.Parse(start);
        var endTimeSpan = TimeSpan.Parse(end);
        
        var startTime = DateTime.Now.Date.AddDays(backOffDay).Add(startTimeSpan);
        var endTime = DateTime.Now.Date.AddDays(backOffDay).Add(endTimeSpan);
        
        Console.WriteLine(startTime.ToString("dd/MM/yyyy H:mm:ss.FFFF"));
        Console.WriteLine(endTime.ToString("dd/MM/yyyy H:mm:ss.FFFF"));
    }
}

You can test it with this fiddle.

19/06/2020 0:00:00.0001

19/06/2020 23:59:59.999

P.S.: To make sure all timestamps are always parsed the same, you might want to do something like this as well:

CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");

or just for a single call:

var culture = new CultureInfo("en-US");
var startTimeSpan = TimeSpan.Parse(start, culture);

Although parsing seemed to work well with en-US culture I wouldn't guarantee it does in all enivronments.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this beautiful answer!. Is var end = "23:59:59.9999"; or it should only contain three 999 correct ?
You're welcome - I updated my answer and the fiddle to use ".FFFF" instead of ".ffff". This will omit trailing zeros. P.S.: You didn't clearly specify "backOffDate", if you want it to be one day ago just use -1 * backOffDay instead of backOffDay.
2

Use DateTime.Now.Date, then add the start and end time to create the start date and end date respectively. Use TimeSpan.Parse to parse the times.

(DateTime.Now.Date + TimeSpan.Parse("13:01:05")).ToString();
//2020-06-18 13:01:05

4 Comments

I need to concatenate the date and time together. And then obtain the datetime as 17/06/2020 00:00:00.0001
@CadaMerx Adding the date-portion of a DateTime and a timespan does exactly that.
An example please ?
@CadaMerx just do what JonasH suggested, then either print the date time or debug and inspect it's value. It will have the times you want on the current date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.