0

i know that the datetime.utcnow command is to show the date and time of the instant it was called.

just say i have this

int time;
time = //27/08/2009 09:00:00

time = time + //15 seconds

what is the code or the numbers i need to use for this?

4
Contributors
11
Replies
12
Views
8 Years
Discussion Span
Last Post by ema005
Featured Replies
  • 1
    ddanbe 2,654   8 Years Ago

    Use the TimeSpan class and the overloaded + operator. [CODE=c#]TimeSpan FifteenSecs = new TimeSpan(0, 0, 15); time = time + FifteenSecs;[/CODE] Read More

  • 1
    sknake 1,622   8 Years Ago

    Is it static except for the date and time changing? Read More

  • 1
    ddanbe 2,654   8 Years Ago

    If you know your string is ALWAYS of the same format you could use: [CODE=c#] string input = "DATA COLLECTED FOR 24 HOURS STARTING AT: 09:00:00 Hrs 27-08-09"; string dateStr = input.Substring(input.Length - 8, 8); string timeStr = input.Substring(input.Length - 21, 8); string datetimeStr = dateStr + " " + … Read More

1

Use the TimeSpan class and the overloaded + operator.

TimeSpan FifteenSecs = new TimeSpan(0, 0, 15);
time = time + FifteenSecs;
Votes + Comments
thats how you do it :)
0

In addition to the timespan you can also add dates using the DateTime class:

DateTime utcNow = DateTime.UtcNow.AddSeconds(15);
0

cheers thats helped, i have a part of a string that has the date in it, i can extract it but how do i use that as the start time.

like how would 27/08/2009 09:00:00 actually be put into "time" so that it timespan actually adds 15 seconds on to it

0

In your first code sample you used an int as type for your time variable, that is not correct.
Use this (just as Scott suggested already) DateTime time = DateTime.UtcNow;
Now you can say things like time.AddSeconds(15); or use a TimeSpan like I did.

0

i have this in a string:

"DATA COLLECTED FOR 24 HOURS STARTING AT: 09:00:00 Hrs 27-08-09"

how would i go about making it "27-08-09 09:00:00"

i have a rough idea with splits and trims etc but i dont actually know how i would get both beside each other. perhaps putting each word in a string, finding out which ones it is then printing out a string made up of the elements containing them?

0

yeah it is, each file comes in with same string, but different time and date.

1

If you know your string is ALWAYS of the same format you could use:

string input = "DATA COLLECTED FOR 24 HOURS STARTING AT: 09:00:00 Hrs 27-08-09";
            string dateStr = input.Substring(input.Length - 8, 8);
            string timeStr = input.Substring(input.Length - 21, 8);
            string datetimeStr = dateStr + " " + timeStr;
0

cheers i did something similar, i have a parts of my code triming and spliting the code up so i just copied on of those, pointed it at the right string and populated a new array with the values, then found which elements had the values i wanted then made

time = (array[9] + " " + array[7];

cheers and thanks for all your help

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.