-2

I want to convert the system date time to a specific format. My system format is dd/mm/yy which i wanted to convert to mm/dd/yyyy and so i am using StrToDateDef. I need to use StrToDateDef only because the date comes as string and if there is a string other than date i will use default date. My code is below

  str := '30/01/14';

  GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, FmtStngs);
  FmtStngs.DateSeparator := '/';
  FmtStngs.ShortDateFormat := 'mm/dd/yyyy';
  FmtStngs.TimeSeparator := ':';
  FmtStngs.LongTimeFormat := 'hh:nn';

  date := StrToDateDef(str,01/28/2013,FmtStngs);

I am expecting the date to be of '01/30/2014' but it is coming as '30/01/14'. What is that i am doing wrong?

Jeeva
  • 4,585
  • 2
  • 32
  • 56
  • You don't explain why the code does not meet your expectations. What is wrong with 30/01/14? – David Heffernan Jan 30 '14 at 07:25
  • The problem is i am assigning maxdatetime (12/31/9999) as default and comparing it in another function. if the year is 2 digits then 99 != 9999 – Jeeva Jan 30 '14 at 07:27
  • I still cannot understand. Why don't you use code to express the failure? – David Heffernan Jan 30 '14 at 07:29
  • @DavidHeffernan: The problem is i cant do it through code. If the default date is 12/31/9999 but it is storing as 31/12/99 and if the actual date itself is 31/12/99 (1999) i will not be able to distinguish – Jeeva Jan 30 '14 at 07:33
  • Well I hope somebody else can make sense of this because I cannot. Never mind. – David Heffernan Jan 30 '14 at 07:36
  • @DavidHeffernan: sorry that i could not explain clearer. any solution to format year to 4 digits? – Jeeva Jan 30 '14 at 07:38
  • `01/28/2013` = 0 - i think you meant something else. // try `'mm/dd/yy'` pattern for two figures year – Arioch 'The Jan 30 '14 at 07:42
  • @Arioch'The I want 4 figures year even though the input is 2 figure year. – Jeeva Jan 30 '14 at 07:44
  • @LURD: That works but when i convert the string back to date it is coming as dd/mm/yy. I want to have it in Tdatetime variable – Jeeva Jan 30 '14 at 07:45
  • 1
    `TDateTime` does not rely on any particular format...you need to use the right format settings in **both** directions. – jpfollenius Jan 30 '14 at 07:46
  • @DavidHeffernan The topic-starter set 4-digits year pattern for ShortDateFormat, but wants Delphi to use 2-digits year format despite his setting. And in the grand picture it wants a single Delphi function to parse both 2-digits and 4-digits year string representations – Arioch 'The Jan 30 '14 at 07:47
  • "I want 4 figures year" that is what you should care about when doign the opposite translation: double -> string; ( TDateTime == double ). "the input is 2 figure year." and i repeat, use ".../yy" format to parse "two-digits input". // I also want to repeat that you use very exotic way to write `date := StrToDateDef(str, 0.0 , FmtStngs);` - it is so exotic that i'm afraid it is not what you wanted to. – Arioch 'The Jan 30 '14 at 07:50
  • 4
    @jeeva You appear to claim that StrToDateDef returns a string. Honestly, if you would just use code to express things, with an SSCCE, there'd be no problem. Why do you not want to ask a clear question? Can't you see how doing so would help you? – David Heffernan Jan 30 '14 at 08:05
  • Jeeva, ISTM that you are under the impression that dates are stored as strings. They are not. I will not explain how they are stored, but the differences in format you are seeing are probably caused by the debugger using a different output format than you are expecting. The internal representation does not change, only the string representation. How it looks to the outside world depends on your settings for the formatting routines you are using. But David is right. Show your exact code (what you show is not!) and why it is not working. In other words: simply copy and paste it from your editor. – Rudy Velthuis Jan 30 '14 at 08:27
  • Oops now i understand that datatype better. Thank you all – Jeeva Jan 30 '14 at 08:53

1 Answers1

7

There are several bugs in this code:

First the TFormatSettings you are passing to the StrToDateDef routine are the format settings for the string you are passing (and not for the datetime variable that comes out, more on that later).

As you are passing '30/01/14' your ShortDateFormat should be 'dd/mm/yyyy' and not 'mm/dd/yyyy'

Then the default value you are passing equals to like 1ms after midnight of 30.12.1899 (because you actually are passing 1 divided by 28 divided by 2014). Use EncodeDate(2013, 1, 28) from DateUtils.pas.

Then you are saying

I am expecting the date to be of '01/30/2014' but it is coming as '30/01/14'.

Well you are looking at a TDateTime variable and it will be formatted according to your local settings of your windows system by the debugger. Nothing more. You don't have a string but a float value (which is what TDateTime is) that is presented as string to you to make it readable.

Also I think the result should be the passed default date because the passed ShortDateFormat does not match the value of the string you are passing (trying to put 30 into the month part).

Stefan Glienke
  • 20,860
  • 2
  • 48
  • 102