Home > front end >  How to add 5 minutes to the string url link date time part?
How to add 5 minutes to the string url link date time part?

Time:10-23

I want to create a future download link from a current link. Not sure if I even need the RoundTime method.

This is an example of the lastRadarLink content how it is built:

https://sites/default/files/ims_data/map_images/IMSRadar/IMSRadar_202210221655.gif

This is the date time part: 202210221655 2022 , 10/22 16:55.

I want to add to the 16:55 more 5 minutes and to rebuild a new link with the added 5 minutes so the next link in this example should be:

https://sites/default/files/ims_data/map_images/IMSRadar/IMSRadar_202210221700.gif

private void DownloadNext(string lastRadarLink)
{

}

and the RoundTime method

DateTime RoundTime(DateTime date, TimeSpan interval)
{
    return new DateTime(date.Ticks / interval.Ticks *
        interval.Ticks);
}

CodePudding user response:

You made it so complicated. In your case the easiest way is to use date and time format strings.

For example:

using System.Globalization;

var time = DateTime.ParseExact("2022-10-22 16:55", "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
var fileName1 = $"XYZ_{time:yyyyMMddHHmm}.gif";
time = time.AddMinutes(5);
var fileName2 = $"XYZ_{time:yyyyMMddHHmm}.gif";
Console.WriteLine(fileName1);
Console.WriteLine(fileName2);

The output is:

XYZ_202210221655.gif
XYZ_202210221700.gif

You can read more about it at https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings


Replying to your comment, you can extract the date from your input string as below

using System.Globalization;

var input = "sites/default/files/ims_data/map_images/IMSRadar/IMSRadar_202210221700.gif";
var timeString = input.Split("/")[^1][9..^4]; // timeString is here "202210221700"
var time = DateTime.ParseExact(timeString, "yyyyMMddHHmm", CultureInfo.InvariantCulture);
var fileName1 = $"XYZ_{time:yyyyMMddHHmm}.gif";
time = time.AddMinutes(5);
var fileName2 = $"XYZ_{time:yyyyMMddHHmm}.gif";
Console.WriteLine(fileName1);
Console.WriteLine(fileName2);

The output will be:

XYZ_202210221700.gif
XYZ_202210221705.gif
  • Related