3 Replies - 1337 Views - Last Post: 09 August 2010 - 12:37 PM Rate Topic: -----

#1 Henningfeld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-February 09

Date and Times in C#

Posted 09 August 2010 - 12:12 PM

I have never tried to do anything like this before and I'm having a hard time seeing on a good path to do so. I have two date pickers ones is where the users will select a date such as 8/9/2010 and another where a user will select a time such as 2:11 P:M. I want to some how combine these two into one date in the back end. I have a date different funcation and would like to combine this date and time together to use it in a function there fore when I pass it I would like for it to be 8/9/2010 2:11 P:M.

This post has been edited by Henningfeld: 09 August 2010 - 12:22 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Date and Times in C#

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1511
  • View blog
  • Posts: 5,916
  • Joined: 21-March 08

Re: Date and Times in C#

Posted 09 August 2010 - 12:19 PM

try this...

DateTime date = datePicker.Value;
DateTime time = timePicker.Value;

DateTime selectedDate = new DateTime(date.Month, date.Day, date.Year, time.Hour, time.Minute, time.Second);

MessageBox.Show(selectedDate.ToString());



I didn't test this code, but you can try it to see if it gives you what you want.
Was This Post Helpful? 2
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3792
  • View blog
  • Posts: 6,387
  • Joined: 08-June 10

Re: Date and Times in C#

Posted 09 August 2010 - 12:20 PM

Well, if you have two DateTimes, you can do something like this:
DateTime date = new DateTime(2010, 8, 9, 0, 0, 0);
DateTime time = new DateTime(2010, 1, 1, 8, 30, 0);
string newDate = string.Format("{0} {1}",
    date.ToString("MM/dd/yyyy"),
    time.ToString("HH:mm"));
DateTime myNewDate = DateTime.Parse(newDate);
Console.WriteLine(myNewDate.ToString());
//output is 8/9/2010 8:30:00 AM
Console.ReadKey();

This post has been edited by insertAlias: 09 August 2010 - 12:21 PM

Was This Post Helpful? 2
  • +
  • -

#4 Henningfeld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-February 09

Re: Date and Times in C#

Posted 09 August 2010 - 12:37 PM

Thanks to both of you. I was making it more diffuclt then what it needed to be.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1