[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 383
  • Last Modified:

DateTime.Now Returns Odd Format with C#

Hi,
I'm adding a feature to some existing code and for some reason when I grab the current date and time to stuff into the database I get a format that is odd to me.

this:
DateTime CreatedDateTime = DateTime.Now;

produces this:
10\/25\/2011 5:35:45 PM

I don't understand where the forward and backward slashes come from, and I've tried to replace them using String.Replace with no success.

Why would the format come back this way, and why can't I programmatically remove them?

Cheers,
Ty
0
MedtronicVascSR
Asked:
MedtronicVascSR
  • 3
1 Solution
 
käµfm³d 👽Commented:
How are you viewing this data?
0
 
MedtronicVascSRAuthor Commented:
Hi,
In order to determine why the SQL insert statement wasn't working I outputed the SQL string in Fidler. Here's a truncated version of that SQL statement with the key info bolded. The current program requires that the date be generated from the code and inserted, and not auto-created from within the table at the time it's inserted (don't ask me why).

INSERT INTO PHYSICIAN_IN_SERVICE ( FIRST_NAME, MIDDLE_INITIAL, LAST_NAME, EMAIL_ADDRESS, PHYSICIAN_INFO_ID, ACCOUNT_NUMBER, CREATED_DATE) VALUES ( 'Ty', '', 'Strange', 'neo@trinity.com', 7777, '1209188', 10\/20\/2011 2:52:37 PM,

The code block I'm working with does reside within a web service that has been posted with Json data, but since the DateTime stamp is not from the HTTP post but rather being generated on it's own I'm don't think that's the issue.

Cheers,
Ty
0
 
MedtronicVascSRAuthor Commented:
Hi,
While I didn't figure out where the slashes came from I did solve my issue by rewriting the code to store the date in a string instead of DateTime before inserting into the Db.

string CreatedDateTime = DateTime.Now.ToString("M-d-yyyy h:mm:ss tt");

Cheers,
Ty
0
 
MedtronicVascSRAuthor Commented:
I was able to solve my issue before others had a chance to post. As for the B grade it wasn't necessarily the only or best way to address the issue.
0

Featured Post

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.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now