[a2-rev6]: datepicker getvalue() Format? - Ext JS

Given:
var myDate = new Ext.form.DateField({format: 'Y-m-d', target: 'mytarget'});

mydate.getvalue returns: Wed Mar 07 2007 00:00:00 GMT+0100

So if i want the format as given in the format property, i need to do:
mydate.formatDate(myDate.getValue())) : 2007-03-07

Is there a shorthand or can Datepicker return an ISO style format per default like: '2007-11-07 13:14:35'?
  # 2  
03-03-2007, 04:28 PM

getValue returns a Date object (not a string).

In the next rev there's a new function getRawValue() which always returns a string. In the meantime, you can use myDate.el.getValue() and just get the value of the text field (which is formatted).
  # 3  
03-03-2007, 05:02 PM

Thank you.
  # 4  
03-06-2007, 05:59 AM

That helps me, that i have found anywhere at web but i dunno where. Is a simple Date-Extension for any date related task, render it to a string over a mask!

// Global Now() function
function Now() {return new Date();}

// Global Constants
var JANUARY   = 0;
var FEBRUARY  = 1;
var MARCH     = 2;
var APRIL     = 3;
var MAY       = 4;
var JUNE      = 5;
var JULY      = 6;
var AUGUST    = 7;
var SEPTEMBER = 8;
var OCTOBER   = 9;
var NOVEMBER  = 10;
var DECEMBER  = 11;

var SUNDAY    = 0;
var MONDAY    = 1;
var TUESDAY   = 2;
var WEDNESDAY = 3;
var THURSDAY  = 4;
var FRIDAY    = 5;
var SATURDAY  = 6;

// Conversion factors as varants to eliminate all the multiplication
var SECONDS_CF     = 1000;
var MINUTES_CF     = 60000;          // 60 * 1000
var HOURS_CF       = 3600000;        // 60 * 60 * 1000
var DAYS_CF        = 86400000;       // 24 * 60 * 60 * 1000
var WEEKS_CF       = 604800000;      // 7 * 24 * 60 * 60 * 1000
var FORTNIGHTS_CF  = 1209600000;     // 14 * 24 * 60 * 60 * 1000
var MONTHS_CF      = 2592000000;     // 30 * 24 * 60 * 60 * 1000  (approx = 1 month)
var QUARTERS_CF    = 7776000000;     // 90 * 24 * 60 * 60 * 1000  (approx = 3 months)
var YEARS_CF       = 31557600000;    // 365 * 24 * 60 * 60 * 1000 (approx = 1 year)
var DECADES_CF     = 315576000000;   // 10 * 365 * 24 * 60 * 60 * 1000 (approx = 1 decade)
var CENTURIES_CF   = 3155760000000;  // 100 * 365 * 24 * 60 * 60 * 1000 (approx = 1 century)

// Non-destructive instance methods
Date.prototype.addMilliseconds = function(ms){  
  return new Date(new Date().setTime(this.getTime() + (ms)));  
}
Date.prototype.addSeconds = function(s){
  return this.addMilliseconds(s * SECONDS_CF);
}
Date.prototype.addMinutes = function(m){
  return this.addMilliseconds(m * MINUTES_CF);
}
Date.prototype.addHours = function(h){
  return this.addMilliseconds(h * HOURS_CF);
}
Date.prototype.addDays = function(d){
  return this.addMilliseconds(d * DAYS_CF);  
}
Date.prototype.addWeeks = function(w){
  return this.addMilliseconds(w * WEEKS_CF);  
}
Date.prototype.addFortnights = function(w){
  return this.addMilliseconds(w * FORTNIGHTS_CF);  
}
Date.prototype.addMonths = function(mm){
  return this.addMilliseconds(mm * MONTHS_CF);  
}
Date.prototype.addYears = function(yy){
  return this.addMilliseconds(yy * YEARS_CF);  
}
Date.prototype.subtractMilliseconds = function(ms){  
  return new Date(new Date().setTime(this.getTime() - (ms)));   
}
Date.prototype.subtractSeconds = function(s){
  return this.subtractMilliseconds(s * SECONDS_CF);
}
Date.prototype.subtractMinutes = function(m){
  return this.subtractMilliseconds(m * MINUTES_CF);
}
Date.prototype.subtractHours = function(h){
  return this.subtractMilliseconds(h * HOURS_CF);
}
Date.prototype.subtractDays = function(d){
  return this.subtractMilliseconds(d * DAYS_CF);  
}
Date.prototype.subtractWeeks = function(w){
  return this.subtractMilliseconds(w * WEEKS_CF);  
}
Date.prototype.subtractFortnights = function(w){
  return this.subtractMilliseconds(w * FORTNIGHTS_CF);  
}
Date.prototype.subtractMonths = function(mm){
  return this.subtractMilliseconds(mm * MONTHS_CF);  
}
Date.prototype.subtractYears = function(yy){
  return this.subtractMilliseconds(yy * YEARS_CF);  
}

// Destructive instance methods (change the value of the current Date object)
Date.prototype._addMilliseconds = function(ms){  
  this.setTime(this.getTime() + (ms));  
  return this;
}
Date.prototype._addSeconds = function(s){
  return this._addMilliseconds(s * SECONDS_CF);  
}
Date.prototype._addMinutes = function(m){
  return this._addMilliseconds(m * MINUTES_CF);
}
Date.prototype._addHours = function(h){
  return this._addMilliseconds(h * HOURS_CF);
}
Date.prototype._addDays = function(d){
  return this._addMilliseconds(d * DAYS_CF);  
}
Date.prototype._addWeeks = function(w){
  return this._addMilliseconds(w * WEEKS_CF);  
}
Date.prototype._addFortnights = function(w){
  return this._addMilliseconds(w * FORTNIGHTS_CF);  
}
Date.prototype._addMonths = function(mm){
  return this._addMilliseconds(mm * MONTHS_CF);  
}
Date.prototype._addYears = function(yy){
  return this._addMilliseconds(yy * YEARS_CF);  
}
Date.prototype._subtractMilliseconds = function(ms){  
  this.setTime(this.getTime() - (ms));  
  return this;
}
Date.prototype._subtractSeconds = function(s){
  return this._subtractMilliseconds(s * SECONDS_CF);
}
Date.prototype._subtractMinutes = function(m){
  return this._subtractMilliseconds(m * MINUTES_CF);
}
Date.prototype._subtractHours = function(h){
  return this._subtractMilliseconds(h * HOURS_CF);
}
Date.prototype._subtractDays = function(d){
  return this._subtractMilliseconds(d * DAYS_CF);  
}
Date.prototype._subtractWeeks = function(w){
  return this._subtractMilliseconds(w * WEEKS_CF);  
}
Date.prototype._subtractFortnights = function(w){
  return this._subtractMilliseconds(w * FORTNIGHTS_CF);  
}
Date.prototype._subtractMonths = function(mm){
  return this._subtractMilliseconds(mm * MONTHS_CF);  
}
Date.prototype._subtractYears = function(yy){
  return this._subtractMilliseconds(yy * YEARS_CF);  
}
Date.prototype.getMonthName = function() {
  var index = (0 == arguments.length) ? this.getMonth() : arguments[0];
  switch(index)
  {
    case JANUARY: 
      return "January";
    case FEBRUARY: 
      return "February";
    case MARCH: 
      return "March";
    case APRIL: 
      return "April";
    case MAY: 
      return "May";
    case JUNE: 
      return "June";
    case JULY: 
      return "July";
    case AUGUST: 
      return "August";
    case SEPTEMBER: 
      return "September";
    case OCTOBER: 
      return "October";
    case NOVEMBER: 
      return "November";
    case DECEMBER: 
      return "December";
    default:
      throw "Invalid month index: " + index.toString();
  }
}
Date.prototype.getMonthAbbreviation = function() {
  var index = (0 == arguments.length) ? this.getMonth() : arguments[0];
  switch(index)
  {
    case JANUARY: 
      return "Jan";
    case FEBRUARY: 
      return "Feb";
    case MARCH: 
      return "Mar";
    case APRIL: 
      return "Apr";
    case MAY: 
      return "May";
    case JUNE: 
      return "Jun";
    case JULY: 
      return "Jul";
    case AUGUST: 
      return "Aug";
    case SEPTEMBER: 
      return "Sep";
    case OCTOBER: 
      return "Oct";
    case NOVEMBER: 
      return "Nov";
    case DECEMBER: 
      return "Dec";
    default:
      throw "Invalid month index: " + index.toString();
  }
}
Date.prototype.getDayName = function() {
  var index = (0 == arguments.length) ? this.getDay() : arguments[0];
  switch(index)
  {
    case SUNDAY: 
      return "Sunday";
    case MONDAY: 
      return "Monday";
    case TUESDAY: 
      return "Tuesday";
    case WEDNESDAY: 
      return "Wednesday";
    case THURSDAY: 
      return "Thursday";
    case FRIDAY: 
      return "Friday";
    case SATURDAY: 
      return "Saturday";    
    default:
      throw "Invalid day index: " + index.toString();
  }
}
Date.prototype.getDayAbbreviation = function() {
  var index = (0 == arguments.length) ? this.getDay() : arguments[0];
  switch(index)
  {
    case SUNDAY: 
      return "Sun";
    case MONDAY: 
      return "Mon";
    case TUESDAY: 
      return "Tue";
    case WEDNESDAY: 
      return "Wed";
    case THURSDAY: 
      return "Thu";
    case FRIDAY: 
      return "Fri";
    case SATURDAY: 
      return "Sat";
    default:
      throw "Invalid day index: " + index.toString();
  }
}
Date.prototype.getCivilianHours = function() {
  return (this.getHours() < 12) ? this.getHours() : this.getHours() - 12;
}
Date.prototype.getMeridiem = function() {
  return (this.getHours() < 12) ? "AM" : "PM";
}
Date.prototype.to_s = Date.prototype.toString;
/* 
    Ultra-flexible date formatting

    %YYYY = 4 digit year         (2005)
    %YY   = 2 digit year         (05)
    %MMMM = Month name           (March)
    %MMM  = Month abbreviation   (March becomes Mar)
    %MM   = 2 digit month number (March becomes 03)
    %M    = 1 or 2 digit month   (March becomes 3)
    %DDDD = Day name             (Thursday)
    %DDD  = Day abbreviation     (Thu)
    %DD   = 2 digit day          (09) 
    %D    = 1 or 2 digit day     (9)
    %HH   = 2 digit 24 hour      (13)
    %H    = 1 or 2 digit 24 hour (9)
    %hh   = 2 digit 12 Hour      (01)
    %h    = 1 or 2 digit 12 Hour (01)
    %mm   = 2 digit minute       (02)
    %m    = 1 or 2 digit minute  (2)
    %ss   = 2 digit second       (59)
    %s    = 1 or 2 digit second  (1)
    %nnn  = milliseconds
    %p    = AM/PM indicator
*/
Date.formatString = '%YYYY-%MM-%DD %HH-%mm-%ss';
Date.prototype.format = function(fs) {

   fs = fs.replace(/%YYYY/, this.getFullYear().toString());
   fs = fs.replace(/%YY/, this.getFullYear().toString().substr(2,2));
   
   fs = fs.replace(/%MMMM/, this.getMonthName(this.getMonth()).toString());
   fs = fs.replace(/%MMM/, this.getMonthAbbreviation(this.getMonth()).toString());
   fs = fs.replace(/%MM/, (this.getMonth() + 1) > 9 ? (this.getMonth() + 1).toString() : "0" + (this.getMonth() + 1).toString());
   fs = fs.replace(/%M/, (this.getMonth() + 1).toString());
   
   fs = fs.replace(/%DDDD/, this.getDayName(this.getDay()).toString());
   fs = fs.replace(/%DDD/, this.getDayAbbreviation(this.getDay()).toString());
   fs = fs.replace(/%DD/, this.getDate() > 9 ? this.getDate().toString() : "0" + this.getDate().toString());
   fs = fs.replace(/%D/, this.getDate().toString());
   
   fs = fs.replace(/%HH/, this.getHours() > 9 ? this.getHours().toString() : "0" + this.getHours().toString());
   fs = fs.replace(/%H/, this.getHours().toString());
   fs = fs.replace(/%hh/, this.getCivilianHours() > 9 ? this.getCivilianHours().toString() : "0" + this.getCivilianHours().toString());
   fs = fs.replace(/%h/, this.getCivilianHours());

   fs = fs.replace(/%mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : "0" + this.getMinutes().toString());
   fs = fs.replace(/%m/, this.getMinutes().toString());

   fs = fs.replace(/%ss/, this.getSeconds() > 9 ? this.getSeconds().toString() : "0" + this.getSeconds().toString());
   fs = fs.replace(/%s/, this.getSeconds().toString());

   fs = fs.replace(/%nnn/, this.getMilliseconds().toString());
   fs = fs.replace(/%p/, this.getMeridiem());
   return fs;
}

Date.prototype.getDaysInMonth = function () {

  switch(this.getMonth())
  {
    case JANUARY:
      return 31;
    case FEBRUARY:
      return this.isLeapYear() ? 29 : 28;
    case MARCH:
      return 31;
    case APRIL:
      return 30;
    case MAY:
      return 31;
    case JUNE:
      return 30;
    case JULY:
      return 31;
    case AUGUST:
      return 31;
    case SEPTEMBER:
      return 30;
    case OCTOBER:
      return 31;
    case NOVEMBER:
      return 30;
    case DECEMBER:
      return 31;
  }
}

// Give toString more flexibility
Date.prototype.toString = function() {
  if (0 == arguments.length || 1 < arguments.length) return this.format(typeof this.formatString!='undefined' ? this.formatString : Date.formatString);
//  if (0 == arguments.length || 1 < arguments.length) return this.to_s();
  return this.format(arguments[0].toString());
} 

Date.prototype.isLeapYear = function() {
  if (0 == this.getFullYear() % 400) return true;
  if (0 == this.getFullYear() % 100) return false;
  return (0 == this.getFullYear() % 4) ? true : false;
}

Date.prototype.zeroTime = function() {
  var dt = this.clone();
  dt.setHours(0);
  dt.setMinutes(0);
  dt.setSeconds(0);
  dt.setMilliseconds(0);
  return dt;
}

Date.prototype.noon = function() {
  var dt = this.clone();
  dt.setHours(12);
  dt.setMinutes(0);
  dt.setSeconds(0);
  dt.setMilliseconds(0);
  return dt;
}

Date.prototype.firstDayOfMonth = function() {  
  return new Date(this.getFullYear(), this.getMonth(), 1, 12, 0, 0);
}
Date.prototype.lastDayOfMonth = function() {  
  return new Date(this.getFullYear(), this.getMonth(), this.getDaysInMonth(), 12, 0, 0);
}

Date.prototype.clone = function() {
  var dt = new Date();
  dt.setTime(this.getTime());  
  return dt;
}

// Static methods
Date.yesterday = function()
{  
  return new Date().noon().subtractDays(1);
}
Date.today = function()
{  
  return new Date().noon();
}
Date.tomorrow = function()
{  
  return new Date().noon().addDays(1);
}
Date.lastWeek = function()
{  
  return new Date().noon().subtractWeeks(1);
}
Date.nextWeek = function()
{  
  return new Date().noon().addWeeks(1);
}
Date.lastMonth = function()
{  
  return new Date().noon().subtractMonths(1);
}
Date.nextFortnight = function()
{  
  return new Date().noon().addFortnights(1);
}
Date.lastFortnight = function()
{  
  return new Date().noon().subtractFortnights(1);
}
Date.nextMonth = function()
{   
  return new Date().noon().addMonths(1);
}
Date.lastYear = function()
{  
  return new Date().noon().subtractYears(1);
}
Date.nextYear = function()
{
  return new Date().noon().addYears(1);
}

// Number class instance methods (Rails-inspired)
Number.prototype.millisecondsFromNow = function() {
  return new Date().addMilliseconds(this);  
}
Number.prototype.millisecondFromNow = function() {
  return this.millisecondsFromNow();  
}
Number.prototype.secondsFromNow = function() {
  return new Date().addSeconds(this);  
}
Number.prototype.secondFromNow = function() {
  return this.secondsFromNow();  
}
Number.prototype.minutesFromNow = function() {
  return new Date().addMinutes(this);  
}
Number.prototype.minuteFromNow = function() {
  return this.minutesFromNow();  
}
Number.prototype.daysFromNow = function() {
  return new Date().addDays(this);  
}
Number.prototype.dayFromNow = function() {
  return this.daysFromNow();  
}
Number.prototype.weeksFromNow = function() {
  return new Date().addWeeks(this);  
}
Number.prototype.weekFromNow = function() {
  return this.weeksFromNow();  
}
Number.prototype.fortnightsFromNow = function() {
  return new Date().addFortnights(this);  
}
Number.prototype.fortnightFromNow = function() {
  return this.fortnightsFromNow();  
}
Number.prototype.monthsFromNow = function() {
  return new Date().addMonths(this);  
}
Number.prototype.monthFromNow = function() {
  return this.monthsFromNow();  
}
Number.prototype.yearsFromNow = function() {
  return new Date().addYears(this);  
}
Number.prototype.yearFromNow = function() {
  return this.yearsFromNow();  
}

Number.prototype.millisecondsAgo = function() {
  return new Date().subtractMilliSeconds(this);  
}
Number.prototype.millisecondAgo = function() {
  return this.millisecondsAgo();  
}
Number.prototype.secondsAgo = function() {
  return new Date().subtractSeconds(this);  
}
Number.prototype.secondAgo = function() {
  return this.secondsAgo();  
}
Number.prototype.minutesAgo = function() {
  return new Date().subtractMinutes(this);  
}
Number.prototype.minuteAgo = function() {
  return this.minutesAgo();  
}
Number.prototype.hoursAgo = function() {
  return new Date().subtractHours(this);  
}
Number.prototype.hourAgo = function() {
  return this.hoursAgo();  
}
Number.prototype.daysAgo = function() {
  return new Date().subtractDays(this);  
}
Number.prototype.dayAgo = function() {
  return this.daysAgo();  
}
Number.prototype.weeksAgo = function() {
  return new Date().subtractWeeks(this);  
}
Number.prototype.weekAgo = function() {
  return this.weeksAgo();  
}
Number.prototype.fortnightsAgo = function() {
  return new Date().subtractFortnights(this);  
}
Number.prototype.fortnightAgo = function() {
  return this.fortnightsAgo();  
}
Number.prototype.monthsAgo = function() {
  return new Date().subtractMonths(this);  
}
Number.prototype.monthAgo = function() {
  return this.monthsAgo();  
}
Number.prototype.yearsAgo = function() {
  return new Date().subtractYears(this);  
}
Number.prototype.yearAgo = function() {
  return this.yearsAgo();  
}

// TimeSpan class exposes helper methods 
// for rounding milliseconds to other granularities of time
// Note: These measurements are subject to subtle rounding errors!

// var d1 = (1).dayFromNow();
// var d2 = (10).daysFromNow(); 
// var ts = new TimeSpan(d2 - d1);
// assert(9 == ts.days);
// var ts2 = new TimeSpan(d1 - d2); // order doesn't matter!
// assert(9 == ts2.days); 

function TimeSpan(ms) {
  this.milliseconds = Math.abs(ms);
  this.seconds      = Math.round(this.milliseconds / SECONDS_CF);
  this.minutes      = Math.round(this.milliseconds / MINUTES_CF);
  this.hours        = Math.round(this.milliseconds / HOURS_CF);
  this.days         = Math.round(this.milliseconds / DAYS_CF);
  this.weeks        = Math.round(this.milliseconds / WEEKS_CF);  
  this.fortnights   = Math.round(this.milliseconds / FORTNIGHTS_CF);  
  this.months       = Math.round(this.milliseconds / MONTHS_CF);      // rounding errors!
  this.quarters     = Math.round(this.milliseconds / QUARTERS_CF);    // rounding errors!
  this.years        = Math.round(this.milliseconds / YEARS_CF);       // rounding errors!
  this.decades      = Math.round(this.milliseconds / DECADES_CF);     // rounding errors!  
  this.centuries    = Math.round(this.milliseconds / CENTURIES_CF);   // rounding errors!
}

TimeSpan.prototype.toString = function() {
  return this.milliseconds.toString();
}
  # 5  
03-06-2007, 06:01 AM

Looks like well written code, but very inefficient. The current date parsing and formatting code compiles the parsing code on first pass to a JS function. The performance difference will be so different it is shocking. (I didn't write the date code).
  # 6  
03-06-2007, 07:34 AM

Quote:
Originally Posted by jacksloc
Looks like well written code, but very inefficient. The current date parsing and formatting code compiles the parsing code on first pass to a JS function. The performance difference will be so different it is shocking. (I didn't write the date code).
Lol, yeah, thatz right. I have seen that too. The developer wanted to re-make .net date functions. But its simple to refactor using yui-ext. I dont need that code many times, so at this time it was faster to take whats always exist (for my project-time).

你可能感兴趣的:(prototype,ext,Rails,performance,yui)