Community
Showing results for 
Search instead for 
Do you mean 
Reply

Date formatting in ToString

Accepted Solution Solved
Copper Super Contributor
Posts: 478
Country: Australia
Accepted Solution

Date formatting in ToString

Hi guys

 

I am getting some rather strange results from

 

                    string ipryear = DateTime.Now.Year.ToString("yyyy");
                    string iprmonth = DateTime.Now.Month.ToString("MM");
                    string iprday = DateTime.Now.Day.ToString("dd");

According to all my searches this should set the format of the date to the 4 number year, 2 number month, and 2 number day

eg if its the 2nd of May that would return 02 and 05

 

However it does not, it actually puts in "yyyy" and "MM" and "dd"

 

I have gone to 5+ C# websites even Microsoft.com and it all says thats how you format the date with a ToString

 

Is this a Act SDK issue?

 

Thanks


Jason Dawson Financial Planning
Brisbane | Australia - JDFP Home Page

Act Ver - Act! Premium Version 17.1.169.0, Hot Fix 3
OS - Windows 7 (stand alone machine)
MS Office - Enterprise 2007
Phone - Samsung Galaxy s6

Accepted Solutions
Solution
Accepted by topic author JasonD
‎03-13-2016 10:22 PM
Copper Elite Contributor
Posts: 81
Country: Canada

Re: Date formatting in ToString

[ Edited ]

Why not just use this? You need to just call the .ToString() method on the date object itself rather than the date,month, and year. When you use month or year etc, it changes the object type to integer, and it does not support DateTime text formatting.

 

string ipryear = DateTime.Now.ToString("yyyy");
string iprmonth = DateTime.Now.ToString("MM");
string iprday = DateTime.Now.ToString("dd");

 

Ahsan Khalid

Chief Technology Officer
Keystroke Quality Computing Inc.
www.keystroke.ca

View solution in original post


All Replies
Silver Super Contributor
Posts: 2,328
Country: USA

Re: Date formatting in ToString

Try leaving off the parentheses at the end.  You have what you need with just the "ToString" method I suspect.

 

Stan


If you would like to get more out of ACT! you can find an ACT! Certified Consultant near you by going to:www.act.com/acc.
-------------------------------------------------------------------------------------
Stan Smith
ACT! Certified Consultant
ADS Programming Services, Inc.
(205) 222-1661
www.adsprogramming.com
www.actwebhosting.com
Click Here to Purchase Act!
Silver Super Contributor
Posts: 2,328
Country: USA

Re: Date formatting in ToString

P.S.  I think if you change it to:

 

string ipryear = DateTime.Now.Format("yyyy")

 

you'll also get what you want.  That will probably be more useful to you in the future.

 

Stan


If you would like to get more out of ACT! you can find an ACT! Certified Consultant near you by going to:www.act.com/acc.
-------------------------------------------------------------------------------------
Stan Smith
ACT! Certified Consultant
ADS Programming Services, Inc.
(205) 222-1661
www.adsprogramming.com
www.actwebhosting.com
Click Here to Purchase Act!
Copper Super Contributor
Posts: 478
Country: Australia

Re: Date formatting in ToString

Thanks Stan

But it does not come up with a .Format option, it works with just () but then the day comes out as just 2 for the 2nd and i really need the 02 option as my code checks for specific PDF files that are named like 20160305 (as example)

Jason Dawson Financial Planning
Brisbane | Australia - JDFP Home Page

Act Ver - Act! Premium Version 17.1.169.0, Hot Fix 3
OS - Windows 7 (stand alone machine)
MS Office - Enterprise 2007
Phone - Samsung Galaxy s6
Silver Super Contributor
Posts: 2,328
Country: USA

Re: Date formatting in ToString

Try these:

 

  string ipryear = Strings.Format(Now,"yyyy");
  string iprmonth = Strings.Format(Now,"MM");
  string iprday = Strings.Format(Now,"dd");

 

Stan


If you would like to get more out of ACT! you can find an ACT! Certified Consultant near you by going to:www.act.com/acc.
-------------------------------------------------------------------------------------
Stan Smith
ACT! Certified Consultant
ADS Programming Services, Inc.
(205) 222-1661
www.adsprogramming.com
www.actwebhosting.com
Click Here to Purchase Act!
Copper Super Contributor
Posts: 478
Country: Australia

Re: Date formatting in ToString

Thanks again Stan

it did not like

string ipryear = Strings.Format(Now,"yyyy");

But did accept

string ipryear = Strings.Format("yyyy", Now);

However it still puts "yyyy" into the string

Jason Dawson Financial Planning
Brisbane | Australia - JDFP Home Page

Act Ver - Act! Premium Version 17.1.169.0, Hot Fix 3
OS - Windows 7 (stand alone machine)
MS Office - Enterprise 2007
Phone - Samsung Galaxy s6
Solution
Accepted by topic author JasonD
‎03-13-2016 10:22 PM
Copper Elite Contributor
Posts: 81
Country: Canada

Re: Date formatting in ToString

[ Edited ]

Why not just use this? You need to just call the .ToString() method on the date object itself rather than the date,month, and year. When you use month or year etc, it changes the object type to integer, and it does not support DateTime text formatting.

 

string ipryear = DateTime.Now.ToString("yyyy");
string iprmonth = DateTime.Now.ToString("MM");
string iprday = DateTime.Now.ToString("dd");

 

Ahsan Khalid

Chief Technology Officer
Keystroke Quality Computing Inc.
www.keystroke.ca

Copper Super Contributor
Posts: 478
Country: Australia

Re: Date formatting in ToString

Yippy that worked, so by ripping out the year/month/day anyway then excludes formating

Jason Dawson Financial Planning
Brisbane | Australia - JDFP Home Page

Act Ver - Act! Premium Version 17.1.169.0, Hot Fix 3
OS - Windows 7 (stand alone machine)
MS Office - Enterprise 2007
Phone - Samsung Galaxy s6
Nickel Contributor
Posts: 175
Country: USA

Re: Date formatting in ToString

For reference (in case anyone else stumbles upon this thread), one can combine all of those into one statement, if so desired.

 

dim dateString as String = Now.ToString("MM/dd/yyyy")   'US
dim dateStringRestOfWorld as String = Now.ToString("yyyy.MM.dd")  'Everywhere else

In these examples, the slashes and periods will be part of the resulting string. I'm sure there are other variations in other countries, but I think this shows pretty well that the US is weird when it comes to our "standards".

 

 

MSDN page for custom date/time formats: https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

Bronze Super Contributor
Posts: 1,231
Country: USA

Re: Date formatting in ToString

If you are going to be international use the system culture patterns from the local PC's OS. 

 

Examples below are based on "en-US" which is "English (United States)" in my windows Time and Language settings.

 

// Long Date

Now.ToString(System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat)       returns        "3/15/2016 10:46:44 AM"

 

// Short Date
Now.ToString(System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern) returns "3/15/2016" 

 

Hope this helps more

-- Jim Durkin