Link to home
Start Free TrialLog in
Avatar of cjJosephj
cjJosephj

asked on

Date Time issue

Hi,

I have a datetime issue

At the moment I  get a time value as ‘16/09/2010 06:00:00’ but in my database the time value is 600. Is there anyway I can convert the datetime in my application to be passed to the database as 600 which is 6 am or can i take care of the problem at the backend.

Thanks in advance
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

you can do wherever you want, only have to choose wisely, and choose data types properly.I would do in the presentation layer aka business rules layes6am meaning 6x100 + 0 (min) is to be done by taking the time variable, and using the .Hour() and the .Minute() properties ...
You can use the formatting to format it to your convenience

DateTime.Now.ToString("dd/MM/yyyy HHmm");

Output: "16/09/2010 1737"
And if you're only interested in Time

DateTime.Now.ToString("HHmm");

Output: "1741"
Will it help?

SELECT CONVERT(VARCHAR(20), GETDATE(), 100)
Avatar of cjJosephj
cjJosephj

ASKER

Thanks for your replies

If wanted to use DateTime.Now.ToString("HHmm"); how can I use that in the code below

              else if (cntrl.GetType() == typeof(RadTimePicker))
                    if (((RadTimePicker)cntrl).SelectedDate.ToString().Length > 0)
               {
                        id = ((RadTimePicker)cntrl).ID;
                        value = ((RadTimePicker)cntrl).SelectedDate.ToString();
                    }

                    
               }


Then pass the values to the database like this

   		foreach (KeyValuePair<string, string> p in parameters)
                    {
                        string key = p.Key;
                        string value = p.Value;

                        ///<remarks>Creates the parameter</remarks>
                        cmd.Parameters.AddWithValue(key, value);
                    }

Open in new window

((RadTimePicker)cntrl).SelectedDate.Value.ToString("HHmm")
((RadTimePicker)cntrl).SelectedDate.ToString("HHmm");
I'm expecting that ((RadTimePicker)cntrl).SelectedDate returns a DateTime object. If not, let us know what's the return type.

((RadTimePicker)cntrl).SelectedDate.ToString("HHmm");
((RadTimePicker)cntrl).SelectedDate.Value will return dateTime

so
((RadTimePicker)cntrl).SelectedDate.Value.ToString("HHmm"); will work
I'm not really sure though as I've never used RadTimePicker control.

Lets do it this way then

DateTime dt = ((RadTimePicker)cntrl).SelectedDate;

dt.ToString("HHmm"); // should do the trick
as SelectedDate is NullableDateTime Property in RadTimePicker (http://www.telerik.com/help/aspnet/calendar/radcalendar-telerik.webcontrols.raddatepicker-selecteddate.html) , you should use as vjSoft told. it addition to that:


RadTimePicker rtp = cntrl as RadTimePicker;
if (rtp != null && SelectedDate.HasValue)
{
        id = rtp.ID;
        value = rtp.SelectedDate.Value.ToString("HH:mm") //24 hours Time format
         //use hh:mm for 12 hours format
}

                   
             
Thanks guys the only issue I have is putting the original value back into a session so it displays back on the screen and i have tried

((RadTimePicker)cntrl).DbSelectedDate = Session[ID].ToString("dd/MM/yyyy HHmm");

but that does not seem to work


Also Mathiyazhagan  SelectedDate.HasValue  is not reconized



I have increased the points to 350
RadtpStartTime.FocusedDate() = Convert.ToDateTime(Session[ID].ToString("dd/MM/yyyy HHmm"));

check session id should not be null
or you can use RaddpStartDate.SelectedDate().Value instead of RadtpStartTime.FocusedDate()
RaddpStartDate is my control name, you have to replace it with your control name

((RadTimePicker)cntrl).FocusedDate() = Convert.ToDateTime(Session[ID].ToString("dd/MM/yyyy HHmm"));

or

((RadTimePicker)cntrl).SelectedDate().Value instead of RadtpStartTime.FocusedDate()

Hi,

I was just testing ((RadTimePicker)cntrl).SelectedDate.Value.ToString("HHmm");

it works fine for for time like 18:00,22:00 etc but get Error converting data type nvarchar to datetime.
when i use 06:00 ,07:00 etc
if you meant to pm time, then you can try with

((RadTimePicker)cntrl).SelectedDate.Value.ToString("hhmm")

consider case of hh
this part Convert.ToDateTime(Session[ID].ToString("dd/MM/yyyy HHmm"));
still has the error message  tostring takes 1 argument
ToString will accept dateformat only when, if your object on which you are applying it is a datetime type.

because session return a object type, not datetime type, you cannot apply ToString to it. Sorry, I missed it in my previous message.

You should only use

Convert.ToDateTime(Session[ID])

i get the error message

String was not recognized as a valid DateTime.


((RadTimePicker)cntrl).SelectedDate = Convert.ToDateTime(Session[ID]);
please apply check, Session[ID] should not be null or blank. it should have date value, we are just converting the session[id] in datatime type.
>> String was not recognized as a valid DateTime.


((RadTimePicker)cntrl).SelectedDate = Convert.ToDateTime(Session[ID]); <<

you can do this way

DateTime dt;

if(Session[ID] != null && DateTime.TryParse(Sesstion[ID].ToString(), out dt))
((RadTimePicker)cntrl).SelectedDate = dt;

else

((RadTimePicker)cntrl).SelectedDate = DateTime.Now; // Setting value to default;
Thanks guys for your hel so far


I do actually check for nulls  my code beloW

 {
                    ID = ((RadTimePicker)cntrl).ID;
                    if (Session[ID] != null)
                   
    ((RadTimePicker)cntrl).SelectedDate = Convert.ToDateTime(Session[ID]);


but still get  String was not recognized as a valid DateTime.
novice this code only brings back the current time never the time selected

if(Session[ID] != null && DateTime.TryParse(Sesstion[ID].ToString(), out dt))
((RadTimePicker)cntrl).SelectedDate = dt;

else

((RadTimePicker)cntrl).SelectedDate = DateTime.Now; // Setting value to default;
SOLUTION
Avatar of Gururaj Badam
Gururaj Badam
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
can you please provide value of Session[ID]
the value is 0600
As expected. You're trying to convert 0600 (Time) to DateTime - Date is missing in the value to be converted.

Either append the current date or store the complete DateTime (formatted to your need) in the session.
it will never contert into date time, thats why you are getting this error.

try following

Convert.ToDateTime(DateTime.Now.ToString("dd/MM/YYYY") + Session[ID].ToString().Substring(0,2) + ":" + Session[ID].ToString().Substring(2,2));

 
Convert.ToDateTime(DateTime.Now.ToString("dd/MM/YYYY") + " " + Session[ID].ToString().Substring(0,2) + ":" + Session[ID].ToString().Substring(2,2));
Thanks Guys

I tried the above and got

The string was not recognized as a valid DateTime. There is an unknown word starting at index 6

I have increased the points to 500
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well, this may be my last post if you're not able to resolve it any further.
        private DateTime GetDateFromTime(string _time)
        {
            DateTime now = DateTime.Now;

            int y = now.Year;
            int m = now.Month;
            int d = now.Day;

            int hr = int.Parse((_time.Length == 4) ? _time.Substring(0, 2) : _time.Substring(0, 1));
            int min = int.Parse((_time.Length == 4) ? _time.Substring(2) : _time.Substring(1));

            return Thread.CurrentThread.CurrentCulture.Calendar.ToDateTime(y, m, d, hr, min, 0, 0);
        }

Open in new window

I have made a mistake I want to award points to Novice and vjsoft
Thank you so much guys for all your help
Why have you raised a request to close, you can accept the best answer or the person who contributed more and any other person as assisted and close it.
has your problem solved?
.
The orginal question is

>> At the moment I  get a time value as ‘16/09/2010 06:00:00’ but in my database the time value is 600. Is there anyway I can convert the datetime in my application to be passed to the database as 600 which is 6 am or can i take care of the problem at the backend. <<

The the perfect answer for this part is posted in Comments - 33690896, 33690860. But later the Author kept on extending the question to other areas of interest.

I object what the Author has decided to award.
You told that "I have made a mistake I want to award points to Novice and vjsoft" but you are going to delete question? whats going wrong? is there any virus in your computer? :)
Yes, I too have heard about this when someone wanted to accept a suggest as primary and another as assist, it got messed up and turned out to be other way.

I'm happy as far as the suggestion are helpful to author and they are able to resolve their problems.