954,160 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

date and time format and how to add

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?

666kennedy
Junior Poster in Training
63 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

Use the TimeSpan class and the overloaded + operator.

TimeSpan FifteenSecs = new TimeSpan(0, 0, 15);
time = time + FifteenSecs;
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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

DateTime utcNow = DateTime.UtcNow.AddSeconds(15);
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

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

666kennedy
Junior Poster in Training
63 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 
DateTime.Parse("27/08/2009 09:00:00").AddSeconds(15);
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

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.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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?

666kennedy
Junior Poster in Training
63 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

Is it static except for the date and time changing?

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

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

666kennedy
Junior Poster in Training
63 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

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;
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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

666kennedy
Junior Poster in Training
63 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

go ahead and mark this post as solved.

ema005
Newbie Poster
24 posts since Jan 2006
Reputation Points: 10
Solved Threads: 4
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You