943,699 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 2022
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 7th, 2009
0

date and time format and how to add

Expand Post »
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

C# Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
666kennedy is offline Offline
63 posts
since Mar 2008
Aug 7th, 2009
1

Re: date and time format and how to add

Use the TimeSpan class and the overloaded + operator.
c# Syntax (Toggle Plain Text)
  1. TimeSpan FifteenSecs = new TimeSpan(0, 0, 15);
  2. time = time + FifteenSecs;
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Aug 7th, 2009
0

Re: date and time format and how to add

In addition to the timespan you can also add dates using the DateTime class:
C# Syntax (Toggle Plain Text)
  1. DateTime utcNow = DateTime.UtcNow.AddSeconds(15);
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Aug 7th, 2009
0

Re: date and time format and how to add

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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
666kennedy is offline Offline
63 posts
since Mar 2008
Aug 7th, 2009
0

Re: date and time format and how to add

C# Syntax (Toggle Plain Text)
  1. DateTime.Parse("27/08/2009 09:00:00").AddSeconds(15);
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Aug 7th, 2009
0

Re: date and time format and how to add

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.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Aug 7th, 2009
0

Re: date and time format and how to add

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
666kennedy is offline Offline
63 posts
since Mar 2008
Aug 7th, 2009
1

Re: date and time format and how to add

Is it static except for the date and time changing?
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Aug 7th, 2009
0

Re: date and time format and how to add

yeah it is, each file comes in with same string, but different time and date.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
666kennedy is offline Offline
63 posts
since Mar 2008
Aug 7th, 2009
1

Re: date and time format and how to add

If you know your string is ALWAYS of the same format you could use:
c# Syntax (Toggle Plain Text)
  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;
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: URGENT ADODB.Recordset adVariant problem
Next Thread in C# Forum Timeline: help on dataGridView





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC