Go Back   SmartClient Forums > Technical Q&A
Wiki Register Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 9th Feb 2007, 08:42
tgochenour tgochenour is offline
Registered Developer
 
Join Date: Jan 2007
Posts: 135
Default Date functions

I want to only display the date and strip the time portion from Date.toNormalDate() if the time is 12:00:00 AM.

How can I do this? Is there custom date format templates or can I only use the function references as listed in DateDisplayFormat
Reply With Quote
  #2  
Old 9th Feb 2007, 08:48
tgochenour tgochenour is offline
Registered Developer
 
Join Date: Jan 2007
Posts: 135
Default

Here's how I solved it:

Code:
	var dt = Date.parseInput(value);
	var ret = dt.toNormalDate();
	ret = ret.replace(/ 12:00:00 AM/,"");
	return ret;
Reply With Quote
  #3  
Old 9th Feb 2007, 17:29
Isomorphic Isomorphic is offline
Administrator
 
Join Date: May 2006
Posts: 30,521
Default

Hi tgochenour,
Your approach will work fine.

As you noted we also supply a set of standard other formats you can use with the toNormalDate() method.

For a truly custom format such as the one you describe there are a couple of options:

You can pass a custom formatter function directly into the toNormalDate() method. This function will be executed in the scope of the date object.

Code:
var myDate = new Date();
function customFormatter () {
      return this.getFullYear() + "." + (this.getMonth() +1) + "." +
               this.getDate();
}
isc.warn(myDate.toNormalDate(customFormatter));

Or you can set your custom formatter function up as the standard normal formatter via Date.setNormalDisplayFormat(), like this:

Code:
function customFormatter () {
      return this.getFullYear() + "." + (this.getMonth() +1) + "." +
               this.getDate();
}
Date.setNormalDisplayFormat(customFormatter);
var myDate = new Date();

isc.warn(myDate.toNormalDate(customFormatter));
Of course if you take this approach you will not be able to call date.getNormalDate() in this formatting function, or you'll end up with an infinitely recursive loop. If you did want to take this approach you'd want to call this.toLocaleString() (the default toNormalDate formatter) from within your formatter function instead.
However if you're not coping this in multiple places in your app, it's probably not necessary to rework your approach.
Reply With Quote
  #4  
Old 15th Feb 2007, 09:30
tgochenour tgochenour is offline
Registered Developer
 
Join Date: Jan 2007
Posts: 135
Default

In your second example, did you mean to say:

Code:
Date.setNormalDisplayFormat(customFormatter);
var myDate = new Date();

isc.warn(myDate.toNormalDate(customFormatter));
or should this read:

Code:
Date.setNormalDisplayFormat(customFormatter);
var myDate = new Date();

isc.warn(myDate.toNormalDate());
Reply With Quote
  #5  
Old 15th Feb 2007, 11:21
Isomorphic Isomorphic is offline
Administrator
 
Join Date: May 2006
Posts: 30,521
Default

Sorry - you're right I meant to say

isc.warn(myDate.toNormalDate());

No need to pass the formatter function into the toNormalDate() method as a parameter since it's already been set up as the default.

Thanks for catching that
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search


© 2010,2011 Isomorphic Software. All Rights Reserved