---
title: "Datum formatering i år."
type: "forum-thread"
url: "https://www.webforum.nu/amne/asp/13479-datum-formatering-i-år"
topic: "ASP"
topic_url: "https://www.webforum.nu/amne/asp"
author: "ConnyFox"
published: "2002-01-01T17:17:00.000Z"
updated: "2002-01-01T20:24:00.000Z"
replies: 2
views: 273
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/asp/13479-datum-formatering-i-år"
---

# Datum formatering i år.

## #1 — ConnyFox, 2002-01-01T17:17Z

Jo det är så att jag har ett script som gör att man får ut någons ålder från datumet han är född men jag skulle vilja ha det med en decimal alltså typ 17,7 för nu visar den 17. Någon som kan hjälpa mig med detta?

```
<%
function formatAge(inDate)

  inYear = int(left(inDate,4))
  inMonth = int(right(left(inDate,6),2))
  inDay = int(right(inDate,2))

  nowYear = int(year(now))
  nowMonth = int(month(now))
  nowDay = int(day(now))

  totYear = int(nowYear-inYear)
  totMonth = int(nowMonth-inMonth)
  totDay = int(nowDay-inDay)

  if totMonth > int(-1) then
    if totDay > int(-1) then
      outAge = totYear
    else
      outAge = totYear-1
    end if
  else
    outAge = totYear-1
  end if

  Response.Write(outAge)

end function
%>
```

Permalänk: https://www.webforum.nu/p/13479

## #2 — PatrikB, 2002-01-01T19:03Z

Då kan du inte "sub-typa" dina variabler till int() eftersom alla dina variabler då blir heltal och således avrundas till närmaste heltal.

Själva uträkningen BÖR inte sub-typas

Du kan testa denna funktion som jag har:

```
<%
Function FormatAge(ByVal dDate)
	'## -- koll om det finns ett argument --
	If Len(dDate) = 0 OR IsNull(dDate) Then Exit Function

	'## -- kolla så att argumentet är ett giltigt datum --
	'If Not IsDate(dDate) Then Exit Function

	Dim dteTotalYear
	Dim dteTotalMonths
	Dim dteDate
	Dim dteTotalYears

	dteDate = Date()
	dteTotalYear = Year(dteDate) - Year(dDate)
	dteTotalMonths = Month(dteDate) - Month(dDate)

	dteTotalYears = dteTotalYear + (dteTotalMonths/12)

	'## -- return correct age format --
	FormatAge = FormatNumber(dteTotalYears,1)

End Function

Response.Write FormatAge(CDate("1900-01-01"))
```

cya,
PatrikB

Permalänk: https://www.webforum.nu/p/302262

## #3 — ConnyFox, 2002-01-01T20:24Z

tack det funkade fin fint men jag löste det på ett annat sätt som passade mig lite bättre!

Permalänk: https://www.webforum.nu/p/302263

---

Tråden på webben: https://www.webforum.nu/amne/asp/13479-datum-formatering-i-år
