crazydev
crazydev

Reputation: 575

Remove string part in a variable containing date in c#

I have a string variable that holds the value of "02/04/2018 to 08/04/2018".

string dateRange = "02/04/2018 to 08/04/2018";

I have a function in c# that gets all the date within the range of 02/04/2018 to 08/04/2018 as per below.

public string getDateRange(string dateRange) {
    var selectedDates = new List<DateTime?>();

    for (var date = Convert.ToDateTime("02/04/2018");
         date <= Convert.ToDateTime("08/04/2018");
         date = date.AddDays(1)) {
            selectedDates.Add(date);
        }
    foreach (var date in selectedDates) {
        Console.WriteLine(date);
    }

    return selectedDates;
}

What I want to achieve in this method is to remove the word to in the date range and pass the starting date and ending date separately. Can someone please help ?

Upvotes: 0

Views: 156

Answers (2)

Slaven Tojić
Slaven Tojić

Reputation: 3014

You can use String.Split() to separate the dates:

public string[] separateDates(string dateRange)
{
     string[] dateSplit = dateRange.Split(new string[] { "to" }, StringSplitOptions.RemoveEmptyEntries);
     return new string[]{dateSplit[0].Trim(), dateSplit[1].Trim()};
}

The method returns a string array that holds the first ("02/04/2018") and the second date ("08/04/2018"):

static void Main()
{
     string dateRange = "02/04/2018 to 08/04/2018";
     string[] myDates = separateDates(dateRange);
     string firstDate = myDates[0];//"02/04/2018"
     string secondDate = myDates[1];//"08/04/2018" 
}

EDIT:

I have implemented my method that separates the dates into your method:

public List<DateTime?> getDateRange(string dateRange)
{
    var selectedDates = new List<DateTime?>();
    string[] dateSplit = dateRange.Split(new string[] { "to" }, StringSplitOptions.RemoveEmptyEntries);
    for (var date = Convert.ToDateTime(dateSplit[0].Trim());
            date <= Convert.ToDateTime(dateSplit[1].Trim());
            date = date.AddDays(1))
    {
        selectedDates.Add(date);
    }
    foreach (var date in selectedDates)
    {
        Console.WriteLine(date.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture));
    }

    return selectedDates;
}

The method now returns List<DateTime?> instead of string because the type of selectedDates is List<DateTime?>. I also made a modification to the console output, now the dates are printing to the console in the following format dd/MM/yyyy (e.g. 02/04/2018).

Upvotes: 4

DirtyBit
DirtyBit

Reputation: 16772

LastIndexOf: This method searches strings from the right. It finds the location of the last occurrence of a letter or substring. It is the reversed version of IndexOf.

public static void Main(string[] args)
 {
   string str = "02/04/2018 to 08/04/2018";
   int pos = str.LastIndexOf("to");
   string result = str.Substring(0, pos) + "  " + str.Substring(pos + 2);
   Console.WriteLine(result);
}

Fiddle

Its printing the date as : 02.04.2018 00:00:00. I have specified the string to dd/MM/yyyy. It just removes the 00:00:00 but keeps the dot in between.

Here is the little amendment to fix that:

 Console.WriteLine(date.Value.ToString("dd/MM/yyyy"));

Upvotes: 1

Related Questions