Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Date Time issue

Posted on 2010-09-16
44
Medium Priority
?
1,227 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 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33690796
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
ID: 33690860
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
ID: 33690896
And if you're only interested in Time

DateTime.Now.ToString("HHmm");

Output: "1741"
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33690919
Will it help?

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

Author Comment

by:cjJosephj
ID: 33693074
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
ID: 33693125
((RadTimePicker)cntrl).SelectedDate.Value.ToString("HHmm")
0
 
LVL 8

Expert Comment

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

Expert Comment

by:Gururaj Badam
ID: 33693248
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
ID: 33693270
((RadTimePicker)cntrl).SelectedDate.Value will return dateTime

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

Expert Comment

by:Gururaj Badam
ID: 33693334
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
ID: 33693380
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
ID: 33694965
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
ID: 33695003
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
ID: 33695021
or you can use RaddpStartDate.SelectedDate().Value instead of RadtpStartTime.FocusedDate()
0
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33695047
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
ID: 33695134
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
ID: 33695160
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
ID: 33695200
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
ID: 33695246
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
ID: 33697502
i get the error message

String was not recognized as a valid DateTime.


((RadTimePicker)cntrl).SelectedDate = Convert.ToDateTime(Session[ID]);
0
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33698210
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
ID: 33698431
>> 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
ID: 33698861
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
ID: 33698865
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 800 total points
ID: 33698870
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
ID: 33698876
can you please provide value of Session[ID]
0
 

Author Comment

by:cjJosephj
ID: 33698897
the value is 0600
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33698905
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
ID: 33698916
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
ID: 33698919
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
ID: 33698960
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 1200 total points
ID: 33698974
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
ID: 33699075
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
ID: 33699363
I have made a mistake I want to award points to Novice and vjsoft
0
 

Author Comment

by:cjJosephj
ID: 33699365
Thank you so much guys for all your help
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33699383
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
ID: 33699385
has your problem solved?
0
 

Author Comment

by:cjJosephj
ID: 33699396
.
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33699412
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
ID: 33699420
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
ID: 33700330
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

971 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