Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-17-2011, 04:58 PM   PM User | #1
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
Javascript strtotime.

Hey for some reason I am not able to use this script (http://phpjs.org/functions/strtotime:554) in Mac Safari, how can I make it work in Safari as well as all the other browsers, as Safari is the only one which doesn't seem to work?
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P

Last edited by DJCMBear; 12-17-2011 at 10:03 PM..
DJCMBear is offline   Reply With Quote
Old 12-17-2011, 06:17 PM   PM User | #2
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
Seems to work in Windows Safari — could you please confirm that this is a Mac only problem (in which case I won't be able to help), or, if it's not, provide a test case with expected and actual result?
__________________
.My new Javascript tutorial site: http://reallifejs.com/
.Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
.Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback
venegal is offline   Reply With Quote
Old 12-17-2011, 06:19 PM   PM User | #3
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
I tested it in Windows Safari 5.1.2 and it wouldn't work it would just return false, i used this.

Code:
alert(strtotime('18-12-2011')); // returned false
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Old 12-17-2011, 07:11 PM   PM User | #4
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
It seems the phpjs implementation of strtotime does a lot less than PHP's strtotime does.

This regexp
PHP Code:
match str.match(/^(d{2,4}-d{2}-d{2})(?:s(d{1,2}:d{2}(:d{2})?)?(?:.(d+))?)?$/); 
obviously doesn't like DD-MM-YYYY, so you could either switch to YYYY-MM-DD, or change the regexp, or apply some logic to the string beforehand, so it will fit the regexp.

Generally, I'd say if you're not doing anything more complex than parsing strings containing explicit dates, you probably shouldn't use phpjs's strtotime at all, since it doesn't do much of what you'd expect it to do (those four test cases in the source are rather meager), and its use of Date.parse generates cross browser issues.
__________________
.My new Javascript tutorial site: http://reallifejs.com/
.Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
.Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback
venegal is offline   Reply With Quote
Old 12-17-2011, 07:19 PM   PM User | #5
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
Shockingly I haven't every needed to do things like this before so never needed to, basically I want to be able to turn something like '+1 year 2 months 5 days' etc like strtotime would do but also allow it to have is turn 12-12-2011 to time as well so I can use it within the javascripts Date function.

Something like this.

Code:
function limit (date, limit) {
    var callback, now = new Date();
    if (date) { now = date; }
    now = strtotime(now);
    callback = limit ? strtotime(limit, now) : now;
    return new Date(callback);
}

alert(limit('12-12-2011', '+2 days')); // returns 14-12-2011
alert(limit('12-12-2011', '+13 months')); // returns 12-01-2013
Ruff code so not 100% correctly done.
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Old 12-17-2011, 08:08 PM   PM User | #6
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
A major problem with your limit function (apart from being overly complicated) is that Date(callback) won't work the way you expect, because Javascript's Date constructor takes milliseconds, and not seconds.

As for the date format: If the date strings are not coming from some other data source, just switch to a standard format like MM-DD-YYYY or YYYY-MM-DD, which can be parsed by Javascript's Date itself (which is what phpjs's strtotime tries to do anyway at first).

If your DD-MM-YYYY format is coming from some other data source, just change it to a standard format inside your limit function, so you won't have to monkeypatch strtotime.

This could look like this:

Code:
function limit(date, limit) {
	if (date.match(/^\d{2}-\d{2}-\d{4}$/)) {
		date = date.split('-').reverse().join('-');
	}
	return new Date((limit ? strtotime(limit, strtotime(date)) : strtotime(date)) * 1000); 
}

alert(limit('18-12-2011', '+2 days'));
alert(limit('18-12-2011', '+13 months'));
__________________
.My new Javascript tutorial site: http://reallifejs.com/
.Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
.Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback
venegal is offline   Reply With Quote
Old 12-17-2011, 08:11 PM   PM User | #7
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
How can I get this to work without using phpjs's strtotime function?

Also I did say it wasn't 100% as I couldn't spend much time on it at the time of writing it.
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Old 12-17-2011, 08:58 PM   PM User | #8
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
Quote:
Originally Posted by DJCMBear View Post
How can I get this to work without using phpjs's strtotime function?
There's no way of answering that question without knowing where those strings are coming from, and whether their format is fixed or just some choice you made for no particular reason. If this is just about adding time to a date, and not about parsing time spans, it's trivial:

PHP Code:
var date1 = new Date('2011-12-18');
date1.setDate(date1.getDate() + 2);

var 
date2 = new Date('2011-12-18');
date2.setMonth(date2.getMonth() + 13);

alert(date1);
alert(date2); 
__________________
.My new Javascript tutorial site: http://reallifejs.com/
.Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
.Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback
venegal is offline   Reply With Quote
Old 12-17-2011, 10:04 PM   PM User | #9
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
I have managed to fix it and it now does what I need it to do, thank you for all your help
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:25 PM.


Advertisement
Log in to turn off these ads.