Solved

Date Time issue

Posted on 2010-09-16
44
1,203 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:cjJosephj
  • 15
  • 12
  • 12
  • +2
44 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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 ...
0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
You can use the formatting to format it to your convenience

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

Output: "16/09/2010 1737"
0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
And if you're only interested in Time

DateTime.Now.ToString("HHmm");

Output: "1741"
0
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
Will it help?

SELECT CONVERT(VARCHAR(20), GETDATE(), 100)
0
 

Author Comment

by:cjJosephj
Comment Utility
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

0
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
((RadTimePicker)cntrl).SelectedDate.Value.ToString("HHmm")
0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
((RadTimePicker)cntrl).SelectedDate.ToString("HHmm");
0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
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");
0
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
((RadTimePicker)cntrl).SelectedDate.Value will return dateTime

so
((RadTimePicker)cntrl).SelectedDate.Value.ToString("HHmm"); will work
0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
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
0
 
LVL 10

Expert Comment

by:Mathiyazhagan
Comment Utility
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
}

                   
             
0
 

Author Comment

by:cjJosephj
Comment Utility
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
0
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
RadtpStartTime.FocusedDate() = Convert.ToDateTime(Session[ID].ToString("dd/MM/yyyy HHmm"));

check session id should not be null
0
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
or you can use RaddpStartDate.SelectedDate().Value instead of RadtpStartTime.FocusedDate()
0
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
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()

0
 

Author Comment

by:cjJosephj
Comment Utility
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
0
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
if you meant to pm time, then you can try with

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

consider case of hh
0
 

Author Comment

by:cjJosephj
Comment Utility
this part Convert.ToDateTime(Session[ID].ToString("dd/MM/yyyy HHmm"));
still has the error message  tostring takes 1 argument
0
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
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])

0
 

Author Comment

by:cjJosephj
Comment Utility
i get the error message

String was not recognized as a valid DateTime.


((RadTimePicker)cntrl).SelectedDate = Convert.ToDateTime(Session[ID]);
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
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.
0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
>> 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;
0
 

Author Comment

by:cjJosephj
Comment Utility
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.
0
 

Author Comment

by:cjJosephj
Comment Utility
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;
0
 
LVL 8

Assisted Solution

by:Gururaj Badam
Gururaj Badam earned 200 total points
Comment Utility
It's quite obvious that Session[ID] is not actually in the correct DateTime format. Can you tell what's the content Session[ID] before you try to parse it?

If say, it's something like 600, then it's not in correct format as there's no date associated.
0
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
can you please provide value of Session[ID]
0
 

Author Comment

by:cjJosephj
Comment Utility
the value is 0600
0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
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.
0
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
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));

 
0
 
LVL 8

Expert Comment

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

Author Comment

by:cjJosephj
Comment Utility
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
0
 
LVL 8

Accepted Solution

by:
Mohit Vijay earned 300 total points
Comment Utility
Can you please do not debug the code?

I wrote "YYYY", actually its not a valid format, you should use "yyyy", Are you just copying and pasting our code, please dont mind, but its not good practice. Please check what this whole below string is returning to you

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



0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
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

0
 

Author Comment

by:cjJosephj
Comment Utility
I have made a mistake I want to award points to Novice and vjsoft
0
 

Author Comment

by:cjJosephj
Comment Utility
Thank you so much guys for all your help
0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
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.
0
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
has your problem solved?
0
 

Author Comment

by:cjJosephj
Comment Utility
.
0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
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.
0
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
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? :)
0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
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.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now