# Help setting date fields from text values using parse fx

**URL:** <https://forum.servoy.com/t/help-setting-date-fields-from-text-values-using-parse-fx/3445>\
**Category:** Classic Servoy\
**Created:** [January 12, 2005, 11:09am UTC](https://forum.servoy.com/t/help-setting-date-fields-from-text-values-using-parse-fx/3445 "2005-01-12T11:09:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![drookwood](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@drookwood](https://forum.servoy.com/u/drookwood)\
**Post date:** [January 12, 2005, 11:09am UTC](https://forum.servoy.com/t/help-setting-date-fields-from-text-values-using-parse-fx/3445/1 "2005-01-12T11:09:59Z")

</div>

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

```auto
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

```auto
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:]( "Embarassed")

---

<div class="post-metadata">

**Author:** ![bcusick](https://avatars.discourse-cdn.com/v4/letter/b/ed8c4c/32.png) [@bcusick](https://forum.servoy.com/u/bcusick)\
**Post date:** [January 12, 2005, 5:17pm UTC](https://forum.servoy.com/t/help-setting-date-fields-from-text-values-using-parse-fx/3445/2 "2005-01-12T17:17:30Z")

</div>

You’re close! Try this:

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

```

Hope this helps.

---

<div class="post-metadata">

**Author:** ![patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@patrick](https://forum.servoy.com/u/patrick)\
**Post date:** [October 17, 2005, 2:48pm UTC](https://forum.servoy.com/t/help-setting-date-fields-from-text-values-using-parse-fx/3445/3 "2005-10-17T14:48:45Z")

</div>

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

```auto
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

```auto
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

---

<div class="post-metadata">

**Author:** ![jcompagner](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/jcompagner/32/4889_2.png) [@jcompagner](https://forum.servoy.com/u/jcompagner)\
**Post date:** [October 18, 2005, 11:13am UTC](https://forum.servoy.com/t/help-setting-date-fields-from-text-values-using-parse-fx/3445/4 "2005-10-18T11:13:49Z")

</div>

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](http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Predefined_Core_Objects:Date_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

---

<div class="post-metadata">

**Author:** ![patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@patrick](https://forum.servoy.com/u/patrick)\
**Post date:** [October 18, 2005, 12:36pm UTC](https://forum.servoy.com/t/help-setting-date-fields-from-text-values-using-parse-fx/3445/5 "2005-10-18T12:36:23Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![jcompagner](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/jcompagner/32/4889_2.png) [@jcompagner](https://forum.servoy.com/u/jcompagner)\
**Post date:** [October 18, 2005, 12:43pm UTC](https://forum.servoy.com/t/help-setting-date-fields-from-text-values-using-parse-fx/3445/6 "2005-10-18T12:43:14Z")

</div>

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

for the format string see:

[http://java.sun.com/j2se/1.4.2/docs/api … ormat.html](http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html)

---

<div class="post-metadata">

**Author:** ![patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@patrick](https://forum.servoy.com/u/patrick)\
**Post date:** [October 18, 2005, 2:13pm UTC](https://forum.servoy.com/t/help-setting-date-fields-from-text-values-using-parse-fx/3445/7 "2005-10-18T14:13:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jcompagner](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/jcompagner/32/4889_2.png) [@jcompagner](https://forum.servoy.com/u/jcompagner)\
**Post date:** [October 20, 2005, 11:54am UTC](https://forum.servoy.com/t/help-setting-date-fields-from-text-values-using-parse-fx/3445/8 "2005-10-20T11:54:48Z")

</div>

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.
