date and time format and how to add

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 63
Reputation: 666kennedy is an unknown quantity at this point 
Solved Threads: 0
666kennedy 666kennedy is offline Offline
Junior Poster in Training

date and time format and how to add

 
0
  #1
Aug 7th, 2009
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

  1. int time;
  2. time = //27/08/2009 09:00:00
  3.  
  4. time = time + //15 seconds

what is the code or the numbers i need to use for this?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,964
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 285
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: date and time format and how to add

 
1
  #2
Aug 7th, 2009
Use the TimeSpan class and the overloaded + operator.
  1. TimeSpan FifteenSecs = new TimeSpan(0, 0, 15);
  2. time = time + FifteenSecs;
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,242
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 577
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: date and time format and how to add

 
0
  #3
Aug 7th, 2009
In addition to the timespan you can also add dates using the DateTime class:
  1. DateTime utcNow = DateTime.UtcNow.AddSeconds(15);
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 63
Reputation: 666kennedy is an unknown quantity at this point 
Solved Threads: 0
666kennedy 666kennedy is offline Offline
Junior Poster in Training

Re: date and time format and how to add

 
0
  #4
Aug 7th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,242
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 577
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: date and time format and how to add

 
0
  #5
Aug 7th, 2009
  1. DateTime.Parse("27/08/2009 09:00:00").AddSeconds(15);
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,964
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 285
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: date and time format and how to add

 
0
  #6
Aug 7th, 2009
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 63
Reputation: 666kennedy is an unknown quantity at this point 
Solved Threads: 0
666kennedy 666kennedy is offline Offline
Junior Poster in Training

Re: date and time format and how to add

 
0
  #7
Aug 7th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,242
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 577
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: date and time format and how to add

 
1
  #8
Aug 7th, 2009
Is it static except for the date and time changing?
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 63
Reputation: 666kennedy is an unknown quantity at this point 
Solved Threads: 0
666kennedy 666kennedy is offline Offline
Junior Poster in Training

Re: date and time format and how to add

 
0
  #9
Aug 7th, 2009
yeah it is, each file comes in with same string, but different time and date.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,964
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 285
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: date and time format and how to add

 
1
  #10
Aug 7th, 2009
If you know your string is ALWAYS of the same format you could use:
  1. string input = "DATA COLLECTED FOR 24 HOURS STARTING AT: 09:00:00 Hrs 27-08-09";
  2. string dateStr = input.Substring(input.Length - 8, 8);
  3. string timeStr = input.Substring(input.Length - 21, 8);
  4. string datetimeStr = dateStr + " " + timeStr;
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC