469,903 Members | 1,461 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

DateTime manipulation

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.
Nov 16 '05 #1
7 10739
Patrick B <ne*******@devzoo.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Patrick B <ne*******@devzoo.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
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]
- mv*@spam.guard.caspershouse.com

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:%2****************@TK2MSFTNGP12.phx.gbl...
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]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Patrick B <ne*******@devzoo.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #4
Ahh, I get you...

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

Thanks, that works.
Nov 16 '05 #5
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.Se cond);

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 16 '05 #6
Patrick B <ne*******@devzoo.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Try This

DateTime A = DateTime.Now;

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

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

Ciaran

"Patrick B" <ne*******@devzoo.com> wrote in message
news:ui**************@TK2MSFTNGP09.phx.gbl...
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.

Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Michele Simionato | last post: by
3 posts views Thread by thomasamillergoogle | last post: by
4 posts views Thread by Andrew Wilhite | last post: by
26 posts views Thread by Reny J Joseph Thuthikattu | last post: by
3 posts views Thread by Tim Cowan | last post: by
2 posts views Thread by csharpula csharp | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.