0.0
Low commit activity in last 3 years
No release in over a year
Parse, validate, and generate Swedish Personal Identity Numbers (PINs / Personnummer)
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

swedish-pin

Inline docs

Validate, parse, and generate Swedish Personal Identity Numbers ("PINs", or Personnummer).

API documentation

Installation

Add this to your Gemfile

gem 'swedish-pin'

Then run bundle install.

Usage

require "swedish_pin"

# Validate strings
SwedishPIN.valid?("8507099805") # => true
SwedishPIN.valid?("8507099804") # => false

# Parse numbers to get more information about them, or to normalize display of
# them.
pin = SwedishPIN.parse("8507099805") # => #<SwedishPIN::Personnummer …>
pin.year # => 1985
pin.birthdate # => #<Date: 1985-07-09>

# The 10-digit variant also knows about century separators.
pin.to_s     # => "850709-9805"
pin.to_s(10) # => "850709-9805"
pin.format_short(Date.civil(2025, 12, 1)) # => "850709-9805"
pin.format_short(Date.civil(2085, 12, 1)) # => "850709+9805"

# Use unofficial 12-digit format for a stable string that doesn't change
# depending on today's date when storing it.
pin.to_s(12)    # => "19850709-9805"
pin.format_long # => "19850709-9805"

# You can also generate numbers to use as example data
fake1 = SwedishPIN.generate
fake2 = SwedishPIN.generate(user.birthday)

License

MIT. See LICENSE file for more details.

This project started out as a fork of personnummer/ruby, but has since been almost completely rewritten. Despite this, the original authors retains most of the copyright since this is derivative work.