Help setting date fields from text values using parse fx

I am trying to script an import which involves setting some date fields with imported data stored as strings in a text file. The stored strings are in the format ‘Wed Jan 12 2005 10:58:54 GMT-0000 (GMT)’. I need to convert the strings into dates before putting them into the date fields. The best option for this seems to be the .parse function but I cannot get it work.

If I have a text string as above, how do I use the parse function to convert it to a valid date?

If I enter

var datestr = 'Wed Jan 12 2005 10:58:54 GMT-0000 (GMT)'
var mydate = new Date();
mydate.setTime(datestr.parse())

I simply get a ‘parse is not a function’ error.

Similarly

var datestr = 'Wed Jan 12 2005 10:58:54 GMT-0000 (GMT)'
var mydate = new Date()
mydate.parse(datestr)

returns the same error. I’m sure I’m just getting the syntax or data types wrong somewhere :oops:

You’re close! Try this:

var datestr = new Date('Wed Jan 12 2005 10:58:54 GMT-0000 (GMT)' )

Hope this helps.

Hello,

I have a similar problem. I am writing records to a text file, using a generic method that writes whatever field of whatever field type. Then I need to re-import that record and run into this problem:

  1. When I do something like
record.date_field 

I get for example

Mon Jul 25 16:25:12 CEST 2005

  1. When I then use the example above

var aDate = new Date(‘Mon Jul 25 16:25:12 CEST 2005’)

I get an invalid date.

When instead of step 1 I do

new Date(record.date_field)

I receive

Mon Jul 25 2005 16:25:12 GMT+0200 (CEST)

which can be perfectly used in step 2.

So it looks that the return type of a (date) dataprovider is somewhat different from a typical Java date type.

Can this be confirmed? Is there a solution for my problem that I currently don’t see?

Thanks
Patrick

What you see is just a toString() of a date. that is also locale specific.

i find here some doc about javascript date parsing:

http://developer.mozilla.org/en/docs/Co … ate_Object

What you really want is that utils.dateFormat() would work in 2 directions:

var stringRepresentation = utils.dateFormat(new Date(), ‘“EEE, MMM d, ''yy”’);
var parsedDate = utils.dateFormat(stringRepresentation , ‘“EEE, MMM d, ''yy”’);

I can’t do this for 2.2.1 anymore that one is finalized now but will make it for the next 3.0 and 2.2.2

What you see is just a toString() of a date. that is also locale specific.

And isn’t there a way of returning a non-locale version of he date object? That’d be actually the easiest, because I can do whatever with it.

yes use utils.dateFormat(dateObject, ‘formatstring’)

for the format string see:

http://java.sun.com/j2se/1.4.2/docs/api … ormat.html

Yes, but my original problem was to make this ‘Mon Jul 25 16:25:12 CEST 2005’ a date object. Like this it is only a string.

yes i have fixed this for the next release (2.2.2 or 3.0)
utils.dateFormat() works now 2 ways

date-> string rep
string rep → date

just give the right format string.