Javascript-форум (https://javascript.ru/forum/)
-   Ваши сайты и скрипты (https://javascript.ru/forum/project/)
-   -   birthdateToAge в safari (https://javascript.ru/forum/project/44869-birthdatetoage-v-safari.html)

Nesquik 05.02.2014 00:28

birthdateToAge в safari
 
function birthDateToAge(birthDate) {
	birthDate = new Date(birthDate);
	var now = new Date(),
	age = now.getFullYear() - birthDate.getFullYear();
        return now.setFullYear(1972) < birthDate.setFullYear(1972) ? age - 1 : age;
}


В safari - nan. Что делать? :)

Riim 05.02.2014 06:04

У меня в safari все норм:
function birthDateToAge(birthDate) {
    birthDate = new Date(birthDate);
    var now = new Date(),
    age = now.getFullYear() - birthDate.getFullYear();
        return now.setFullYear(1972) < birthDate.setFullYear(1972) ? age - 1 : age;
}

alert(birthDateToAge(new Date(1986, 2, 3)));


Покажите как запускаете.

Nesquik 05.02.2014 16:35

Да, Riim, твой вроде скрипт из соседней ветки. Дата приходит в формате "19.01.1988" :)

Riim 06.02.2014 14:31

/**
 * @param {string} date
 * @returns {Date}
 */
function parseDate(date) {
	var match = date.match(/\d+/g);

	if (match !== null && match.length === 3) {
		var year = Number((match[0].length > 2 ? match.reverse() : match)[2]);
		var month = match[1] - 1;
		var day = Number(match[0]);
		var date_ = new Date(year, month, day);

		if (date_.getFullYear() === year && date_.getMonth() === month && date_.getDate() === day) {
			return date_;
		}
	}
	return new Date(NaN);
}

/**
 * @see [url]http://javascript.ru/forum/misc/38107-vychislenie-vozrasta.html[/url]
 *
 * @param {Date|int} bd
 * @returns {int}
 */
function birthDateToAge(bd) {
	bd = new Date(bd);
	var now = new Date();
	return now.getFullYear() - bd.getFullYear() - (now.setFullYear(1972) < bd.setFullYear(1972) ? 1 : 0);
}

alert(birthDateToAge(parseDate('19.01.1988')));


Часовой пояс GMT +3, время: 19:16.