11 Replies - 1034 Views - Last Post: 18 March 2016 - 03:12 PM Rate Topic: -----

#1 jphoc13   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 584
  • Joined: 08-July 13

Why is my date not formatting

Posted 15 March 2016 - 12:04 PM

My sExpDate = yyyy-mm-dd before I try to convert it.

 get
            {
                string test = string.Format("{0:dd/MM/yyyy}", this.sExpDate);
                return this.sStrike + "," + this.sPutCall + "," + test;
            }


Yet when I run this code to test if it is working the format doesn't change to dd/MM/yyyy
Is This A Good Question/Topic? 0
  • +

Replies To: Why is my date not formatting

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15797
  • View blog
  • Posts: 63,297
  • Joined: 12-June 08

Re: Why is my date not formatting

Posted 15 March 2016 - 12:11 PM

A string is not a datetime format that the 'string.format' knows where days, months, and years are. To it is is just any other string.

Convert the string to a datetime object then use your string.format.
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Why is my date not formatting

Posted 15 March 2016 - 12:13 PM

What data-type is sExpDate? The 's' (and your description of its format) suggests it is just a string.
Was This Post Helpful? 0
  • +
  • -

#4 jphoc13   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 584
  • Joined: 08-July 13

Re: Why is my date not formatting

Posted 16 March 2016 - 08:20 AM

Sorry I meant to get back to this yesterday but this site was acting funky.

Anyways I ended up going with this:

DateTime vDateParam;
                vDateParam = DateTime.Parse(this.sExpDate);
                string day = vDateParam.ToString("yyyy/MM/dd");
                
                return this.sStrike + "," + this.sPutCall + "," + day;


On a side note I now know why coders can't stand poorly formatted excel sheets and DB's now. Having to search through data to see why something isn't being converted is a pain in the arse!

This post has been edited by jphoc13: 16 March 2016 - 08:33 AM

Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Why is my date not formatting

Posted 16 March 2016 - 08:41 AM

For safety, you should use TryParse instead of Parse.

//make your format string a constant
private const string DateFormatString = "yyyy/MM/dd";

//somewhere else in the code...

DateTime result;
if(!DateTime.TryParse(dateString, out result)){
    //date string failed to parse, set default value
    result = DateTime.MinValue;
}
var formattedDate = result.ToString(DateFormatString);
//use formattedDate


This is "safer", in that if you feed it a bad date, it won't crash (it will use a default value, which is "0001-01-01", so it would be quite obvious that the parsing failed.

Or, if you wanted something more explicit, you could do something like this:

DateTime result;
string formattedDate = "INVALID DATE";
if(DateTime.TryParse(dateString, out result)){
    formattedDate = result.ToString(DateFormatString);
}
//use formattedDate

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7453
  • View blog
  • Posts: 25,095
  • Joined: 05-May 12

Re: Why is my date not formatting

Posted 17 March 2016 - 06:41 AM

View Postandrewsw, on 15 March 2016 - 03:13 PM, said:

What data-type is sExpDate? The 's' (and your description of its format) suggests it is just a string.


It's stringly-typed (see #7)!

Posted Image

View Postjphoc13, on 16 March 2016 - 11:20 AM, said:

:
On a side note I now know why coders can't stand poorly formatted excel sheets and DB's now. Having to search through data to see why something isn't being converted is a pain in the arse!

This should be a learning moment for:
1) Using TryParse() as noted above; and
2) Finding and consistently using logging. (I suggest log4net.)

With the combination of the two, you could have detected that something was poorly formatted, and then put into the log file exactly what the bad value was and what row it was found in.
Was This Post Helpful? 0
  • +
  • -

#7 jphoc13   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 584
  • Joined: 08-July 13

Re: Why is my date not formatting

Posted 18 March 2016 - 01:47 PM

Quick format question..

If I have a double that is coming out 1111100000000000000.00000

And it is supposed to be 1.1111.

What is the best way to format that? How would I move the decimal and remove all those zeroes?
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Why is my date not formatting

Posted 18 March 2016 - 01:49 PM

As stated, that isn't a formatting issue, they are two different numbers.

Why are they two different numbers?
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15797
  • View blog
  • Posts: 63,297
  • Joined: 12-June 08

Re: Why is my date not formatting

Posted 18 March 2016 - 01:50 PM

Divide by 1000000000000000000 would be reasonable.
Was This Post Helpful? 1
  • +
  • -

#10 jphoc13   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 584
  • Joined: 08-July 13

Re: Why is my date not formatting

Posted 18 March 2016 - 01:52 PM

View Postandrewsw, on 18 March 2016 - 01:49 PM, said:

As stated, that isn't a formatting issue, they are two different numbers.

Why are they two different numbers?

I am digging into that issue..... But in the meantime I was looking for a quick fix.
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Why is my date not formatting

Posted 18 March 2016 - 02:08 PM

Erm.. okay. A quick fix would be the last thing on my mind.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7453
  • View blog
  • Posts: 25,095
  • Joined: 05-May 12

Re: Why is my date not formatting

Posted 18 March 2016 - 03:12 PM

If you don't know why something is happening, a quick fix is usually last thing you want to do.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1