This topic is locked
[SOLVED]

Converting string to datetime....

1/18/2023 6:31:58 AM
PHPRunner General questions
D
david powell author

Hi All
I would be really grateful for some guidance... I have been googling and experimenting all morning to no avail.

I have the following bit of code:

Dynamic cult="en-GB";
Dynamic dob="23/12/1983";

data["dob"]=DateTime.ParseExact(dob, "dd/mm/yyyy",cult);
data["user_id"]=user_id.ToString();
data["address1"]=address.ToString();
data["pcode"]=postcode.ToString();
data["location_name"]=location_name.ToString();
data["location_id"]=location_id.ToString();
data["user_id"]=user_id.ToString();
dc = new DsCommand();
dc.values = data;
dataSource = CommonFunctions.getDataSource("phonebook");
dataSource.insertSingle( dc );

What gets inserted is 00/00/000
I have tried just data["dob"]=dob.ToString() and a variety of other options to no avail!

I am sure I am either making it too complex or doing something really silly... but I cant work out what ti is!

David

D
david powell author 1/18/2023

Apologies, I may have put this in the wrong part of the forum... I thought I chose asprunner.net .... its c#

Admin 1/18/2023

I would suggest using Database API to insert data. Not necessarily the source of your issue but it always makes sense to use the recommended API.

Also, where the data in this format coming from? If you use the built-in ASPRunner.NET functionality it will handle date format conversion for you.

D
david powell author 1/18/2023

It's being inserted via a bit of PHP code working as an API and inserting the data from a JSON into a temporary database, then being processed (as above ) and inserted into its final destination. I am using this method because the data is encrypted, and I dont want to have lots of DB.Exec with the key around in the code.

I finally managed to make it work after hours of trial and error

  1. Convert your date string into what seems to be the universally recognised sql format of yyyy-mm-dd by whatever fashion you like, I concatenated together it from three string variables (ye for year, mo for month and da for day) - make sure its int he format 1930-04-05 not 1930-4-5
  2. Add a smattering of ToString() which seem to be necessary, on a rather mysterious and unpredictable basis, even though you have strings already.
  3. Replace my line above with
    data["dob"]=ye.ToString()+ "-" + mo.ToString() + "-" + da.ToString();

Apologies if this is too basic, but it took me all day to work out and I thought I should share it!