PC Review


Reply
Thread Tools Rate Thread

DateTime manipulation

 
 
Patrick B
Guest
Posts: n/a
 
      6th Jan 2005
Say I have three DateTime variables:

DateTime A;
DateTime B;
DateTime C;

How can I assign to C just the date part of A and just the time part of B?

Something like this...

C = A.Date + B.TimeOfDay;

Except that isn't it.
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      6th Jan 2005
Patrick B <(E-Mail Removed)> wrote:
> Say I have three DateTime variables:
>
> DateTime A;
> DateTime B;
> DateTime C;
>
> How can I assign to C just the date part of A and just the time part of B?
>
> Something like this...
>
> C = A.Date + B.TimeOfDay;
>
> Except that isn't it.


I ended up writing a utility method which did something like this -
it's not hard to write once and then use repeatedly. Basically use the
form of the DateTime constructor which takes everything you want, and
take some parts from A and some parts from B.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      6th Jan 2005
Hi,

Another solution could be take the data part of A

a.Date

and the time components of B

b.Second , b.Hour , b.Minute

That should work

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Patrick B <(E-Mail Removed)> wrote:
>> Say I have three DateTime variables:
>>
>> DateTime A;
>> DateTime B;
>> DateTime C;
>>
>> How can I assign to C just the date part of A and just the time part of
>> B?
>>
>> Something like this...
>>
>> C = A.Date + B.TimeOfDay;
>>
>> Except that isn't it.

>
> I ended up writing a utility method which did something like this -
> it's not hard to write once and then use repeatedly. Basically use the
> form of the DateTime constructor which takes everything you want, and
> take some parts from A and some parts from B.
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      6th Jan 2005
Or, if we wanted to be really nifty:

// Get the date and time.
DateTime now = DateTime.Now();

// Get the date portion, and the time.
DateTime date = now.Date;
TimeSpan time = now.TimeOfDay;

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:%(E-Mail Removed)...
> Hi,
>
> Another solution could be take the data part of A
>
> a.Date
>
> and the time components of B
>
> b.Second , b.Hour , b.Minute
>
> That should work
>
> Cheers,
>
> --
> Ignacio Machin,
> ignacio.machin AT dot.state.fl.us
> Florida Department Of Transportation
>
>
>
> "Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Patrick B <(E-Mail Removed)> wrote:
>>> Say I have three DateTime variables:
>>>
>>> DateTime A;
>>> DateTime B;
>>> DateTime C;
>>>
>>> How can I assign to C just the date part of A and just the time part of
>>> B?
>>>
>>> Something like this...
>>>
>>> C = A.Date + B.TimeOfDay;
>>>
>>> Except that isn't it.

>>
>> I ended up writing a utility method which did something like this -
>> it's not hard to write once and then use repeatedly. Basically use the
>> form of the DateTime constructor which takes everything you want, and
>> take some parts from A and some parts from B.
>>
>> --
>> Jon Skeet - <(E-Mail Removed)>
>> http://www.pobox.com/~skeet
>> If replying to the group, please do not mail me too

>
>



 
Reply With Quote
 
Patrick B
Guest
Posts: n/a
 
      6th Jan 2005
Ahh, I get you...

DateTime C = new DateTime(A.Year, A.Month, A.Day, B.Hour, B.Minute,
B.Second);

Thanks, that works.
 
Reply With Quote
 
=?ISO-8859-1?Q?=22Anders_Nor=E5s_=5BMCAD=5D=22?=
Guest
Posts: n/a
 
      6th Jan 2005
Patrick B wrote:
> How can I assign to C just the date part of A and just the time part of B?

Use the DateTime constructor with Year, Month and Day from DateTime a
and Hour, Minute and Second from DateTime b.

DateTime a=new DateTime(2005,01,04);
DateTime b=DateTime.Now;
DateTime c=new DateTime(a.Year,a.Month,a.Day,b.Hour,b.Minute,b.Second);

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      6th Jan 2005
Patrick B <(E-Mail Removed)> wrote:
> Ahh, I get you...
>
> DateTime C = new DateTime(A.Year, A.Month, A.Day, B.Hour, B.Minute,
> B.Second);
>
> Thanks, that works.


Goodo - but actually, looking at your original post, I don't see why
that shouldn't work. For instance:

using System;

class Test
{
static void Main()
{
DateTime now = DateTime.Now;
DateTime yesterday = DateTime.Today.AddDays(-1);
DateTime thisTimeYesterday = yesterday.Date+now.TimeOfDay;

Console.WriteLine(thisTimeYesterday);
}
}

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Ciaran
Guest
Posts: n/a
 
      6th Jan 2005
Try This

DateTime A = DateTime.Now;

DateTime B = DateTime.Now.Add(-1);

DateTime C = A.Date.AddTicks(B.TimeOfDay.Ticks);



Ciaran

"Patrick B" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Say I have three DateTime variables:
>
> DateTime A;
> DateTime B;
> DateTime C;
>
> How can I assign to C just the date part of A and just the time part of B?
>
> Something like this...
>
> C = A.Date + B.TimeOfDay;
>
> Except that isn't it.



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Millisecond values missing when inserting datetime into datetime column of sql Server Manikandan Microsoft C# .NET 4 18th Jul 2007 08:59 PM
How can I save a DateTime from my C# program into a SQL Server (datetime) database column. Steve Kershaw Microsoft ASP .NET 5 29th Aug 2006 03:10 AM
DateTime manipulation Tim Cowan Microsoft C# .NET 3 1st Feb 2006 10:17 PM
For better Performance in VBA for Excel - Strings manipulation OR Objects manipulation vmegha Microsoft Excel Programming 2 19th Dec 2005 12:14 AM
DateTime format/manipulation question Andrew Wilhite Microsoft C# .NET 4 21st Sep 2004 11:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:56 AM.