1 // ========================================================================== 2 // Project: SproutCore - JavaScript Application Framework 3 // Copyright: ©2006-2011 Strobe Inc. and contributors. 4 // Portions ©2008-2011 Apple Inc. All rights reserved. 5 // License: Licensed under MIT license (see license.js) 6 // ========================================================================== 7 8 sc_require('validators/validator') ; 9 10 /** 11 Handle parsing and display of dates. 12 13 @class 14 @extends SC.Validator 15 @author Charles Jolley 16 @version 1.0 17 */ 18 SC.Validator.Date = SC.Validator.extend( 19 /** @scope SC.Validator.Date.prototype */ { 20 21 /** 22 The standard format you want the validator to convert dates to. 23 */ 24 format: '%b %d, %Y %i:%M:%S %p', 25 26 /** 27 if we have a number, then convert to a date object. 28 */ 29 fieldValueForObject: function(object, form, field) { 30 var format = this.get('format'), 31 dateTime; 32 33 /* 34 TODO [CC] deprecated warning, we should remove this in a future release 35 */ 36 // @if (debug) 37 if (format.indexOf('%') === -1) { 38 SC.Logger.warn("You're using a Date validator with a format (%@) for time.js, which has been deprecated. Please change your format to something compatible with SC.DateTime".fmt(format)); 39 format = this.constructor.prototype.format; 40 } 41 // @endif 42 43 if (SC.typeOf(object) === SC.T_NUMBER) { 44 dateTime = SC.DateTime.create(object); 45 } else if (object instanceof Date) { 46 dateTime = object.getTime(); 47 } 48 49 if (dateTime) { object = dateTime.toFormattedString(format); } 50 51 return object; 52 }, 53 54 /** 55 Try to pass value as a date. convert into a number, or return null if 56 it could not be parsed. 57 */ 58 objectForFieldValue: function(value, form, field) { 59 var format = this.get('format'), 60 dateTime; 61 62 /* 63 TODO [CC] deprecated warning, we should remove this in a future release 64 */ 65 // @if (debug) 66 if (format.indexOf('%') === -1) { 67 SC.Logger.warn("You're using a Date validator with a format (%@) for time.js, which has been deprecated. Please change your format to something compatible with SC.DateTime".fmt(format)); 68 format = this.constructor.prototype.format; 69 } 70 // @endif 71 72 if (value) { 73 dateTime = SC.DateTime.parse(value, format); 74 value = dateTime ? dateTime._ms : null; 75 } 76 return value ; 77 } 78 79 }) ; 80