Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

datetime string conversion

Posted on 2007-11-19
22
Medium Priority
?
825 Views
Last Modified: 2013-12-17
I am trying to get the nowGMT into a string !  ... but the following script is not working out for me yet.

what am I doing wrong?

your time and efforts with this enquiry are much apprieated.
DateTime now = DateTime.Now;
DateTime nowGMT = now.ToUniversalTime();
 
string nowGMTstring = nowGMT.ToString();

Open in new window

0
Comment
Question by:amillyard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 5
  • 4
  • +1
22 Comments
 
LVL 23

Assisted Solution

by:Ashish Patel
Ashish Patel earned 960 total points
ID: 20311437
Try this
DateTime now = DateTime.Now();
Date nowGMT = now.ToUniversalTime();
string nowGMTstring = nowGMT.ToString();

Open in new window

0
 

Author Comment

by:amillyard
ID: 20311461
asvforce:

cannot do that change, as 'Date' is then not visible in the namespace.
0
 
LVL 11

Assisted Solution

by:Babycorn-Starfish
Babycorn-Starfish earned 680 total points
ID: 20311526
Just a typo i think in that last code snippet
DateTime now = DateTime.Now;
DateTime nowGMT = now.ToUniversalTime();
string nowGMTstring = nowGMT.ToString();
MessageBox.Show(nowGMTstring);

Open in new window

0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 11

Assisted Solution

by:Babycorn-Starfish
Babycorn-Starfish earned 680 total points
ID: 20311533
However,

i don't see any difference between your original code and that suggested by asvforce.

What were you getting assigned to nowGMTstring in your original code?
0
 

Author Comment

by:amillyard
ID: 20311553
Babycorn-Starfish:

Yes, I agree, no apparent difference -- except the 'Time' text removed.

DateTime now = DateTime.Now();
Date nowGMT = now.ToUniversalTime();   --- nowGMT is setup here and gets the current date/time data

"nowGMT" has all the data I that is required ... just need to convert that to a string format -- so I can make is part of a sql statement (string formatted).

string nowGMTstring = nowGMT.ToString();  -- this was my attempt to convert to string.
0
 
LVL 11

Assisted Solution

by:Babycorn-Starfish
Babycorn-Starfish earned 680 total points
ID: 20311651
Hmm,

well that code does work

>>string nowGMTstring = nowGMT.ToString();

Is it just that it's not giving it to you in a format you can use?, give an example of what you actually get assigned to nowGMTstring and how ytou would like it to appear.

0
 
LVL 23

Assisted Solution

by:Ashish Patel
Ashish Patel earned 960 total points
ID: 20311710
Try using this too. AS your code also worked for me, though please try the below one too.
string nowGMTstring = DateTime.Now().ToUniversalTime().ToString();

Open in new window

0
 

Author Comment

by:amillyard
ID: 20311721
nothing is being updated when posting the database update -- when I take out the nowGMT column -- db updates ok.

[DateTimeStamp] on the db is setup as a 'smalldatetime' in structure.
DateTime now = DateTime.Now;
DateTime nowGMT = now.ToUniversalTime();
string nowGMTstring = nowGMT.ToString();
 
string sql = "UPDATE [MasterAccounts] set [TelemarketingInUseLock]=" + StatusUpdate + "[DateTimeStamp]=" + nowGMT + " WHERE [MasterAccount_ID]=" + (String)Session["MasterAccount"];

Open in new window

0
 
LVL 11

Assisted Solution

by:Babycorn-Starfish
Babycorn-Starfish earned 680 total points
ID: 20311754
If this is your actual code is think the last line has an erorr in it in that your are passing nowGMT instead of nowGMTstring
0
 
LVL 23

Accepted Solution

by:
Ashish Patel earned 960 total points
ID: 20311761
Okay now try this and let me know what error you get if the update is now running good.


DateTime now = DateTime.Now;
DateTime nowGMT = now.ToUniversalTime();
string nowGMTstring = nowGMT.ToString();
 
string sql = "UPDATE [MasterAccounts] set [TelemarketingInUseLock]=" + StatusUpdate + ", [DateTimeStamp]='" + nowGMT + "' 
WHERE [MasterAccount_ID]=" + (String)Session["MasterAccount"];

Open in new window

0
 

Author Comment

by:amillyard
ID: 20311805
Babycorn-Starfish:

yes, it is actual code, I was trying with "nowGMT" and with "nowGMTstring" -- to see if there was any different (i.e. perhaps I did not have to convert to string).

asvforce:

have tried the code update as indicated -- db still refuing to update itself.

if I take out the "", [DateTimeStamp]='" + nowGMT +" for example -- db updates fine.
0
 
LVL 11

Assisted Solution

by:Babycorn-Starfish
Babycorn-Starfish earned 680 total points
ID: 20311874
try with a literal value in the format required, e.g. copy and paste from DB into your code and see whether it'll update that way.
0
 

Author Comment

by:amillyard
ID: 20311981
same issue -- even with literal as follows -- this is prevent a db update
string sql = "UPDATE [MasterAccounts] set [TelemarketingInUseLock]=" + StatusUpdate + "[DateTimeStamp]='19/11/2007 12:24:00' WHERE [MasterAccount_ID]=" + (String)Session["MasterAccount"];

Open in new window

0
 

Author Comment

by:amillyard
ID: 20311986
submitting this sql string -- works fine (but obviously no date/time updating going on)
string sql = "UPDATE [MasterAccounts] set [TelemarketingInUseLock]=" + StatusUpdate + " WHERE [MasterAccount_ID]=" + (String)Session["MasterAccount"];

Open in new window

0
 

Author Comment

by:amillyard
ID: 20311999
in a different table, I can update the date and time ok, (when creating a new record) as follows via stored procedure:   (but obviously no other string conversion / manipulation here)



DateTime now = DateTime.Now;
DateTime nowGMT = now.ToUniversalTime();
scCommand.Parameters.AddWithValue("@DateTimeStamp", nowGMT);

Open in new window

0
 
LVL 23

Assisted Solution

by:Ashish Patel
Ashish Patel earned 960 total points
ID: 20312311
Try changing the field from short date time to just date time and then do. If this fixes your issue then you can modify the update statement to take the date as short date time and then update.Let me know.
0
 
LVL 11

Assisted Solution

by:DotNetThinker
DotNetThinker earned 360 total points
ID: 20312712
try this (be sure to include a space before the [DateTimeStamp]):
string sql = "UPDATE [MasterAccounts] set [TelemarketingInUseLock]=" + StatusUpdate + " [DateTimeStamp]=cast('" + nowGMTstring + "' as shortdatetime) WHERE [MasterAccount_ID]=" + (String)Session["MasterAccount"];

Open in new window

0
 
LVL 11

Assisted Solution

by:DotNetThinker
DotNetThinker earned 360 total points
ID: 20312718
sorry, forgot the comma:


string sql = "UPDATE [MasterAccounts] set [TelemarketingInUseLock]=" + StatusUpdate + ", [DateTimeStamp]=cast('" + nowGMTstring + "' as shortdatetime) WHERE [MasterAccount_ID]=" + (String)Session["MasterAccount"];

Open in new window

0
 
LVL 11

Assisted Solution

by:DotNetThinker
DotNetThinker earned 360 total points
ID: 20312730
...may not need the cast if you include the space and comma.
0
 

Author Comment

by:amillyard
ID: 20317068
not working for me yet ... have restructured the scripting as follows:

webservice file:

    public class datetimeserver : System.Web.Services.WebService
    {
        [WebMethod]
        public DateTime GetServerTime()
        {
            return DateTime.Now;
        }
    }



.cs file


datetimeserver service = new datetimeserver();
DateTime serverTime = service.GetServerTime();

string sql = "UPDATE [MasterAccounts] set [RecordInUseLock]='" + StatusUpdate + "', [DateTimeStamp]='" + serverTime.ToString() + "' WHERE [MasterAccount_ID]=" + (String)Session["MasterAccount"];


cannot get this part to work yet -- ", [DateTimeStamp]='" + serverTime.ToString()

datetimestamp is a smalldatetime variable
0
 

Author Comment

by:amillyard
ID: 20318466
fyi -- I managed to the string version working -- had to change the datetimestamp to a string variable though.

problem is that I am getting to output results for the time part as follows:

3:22:40 AM  - when running scripts via the server
08:19:20   - locally (development platform) ran

what I would to achieve though is:

xx/xx/xxxx xx:xx:xx
day/month/year hour/min/secs

what I also would like to achieve is obviously for this to be updating as a smalldatetime variable in db table.
0
 

Author Comment

by:amillyard
ID: 20333536
Resolved this in the end using the following to correct the formatting issue:

string serverDateTime = serverTime.ToString("MMM dd, yyyy HH:mm:ss");
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 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