Metuuu
4/9/2019 - 10:17 AM

FinnishDate

// Ei toimi AWS Lambdassa

export default class FinnishDate {

	constructor(date) {
		const _date = new Date(date)
		const [ fDate, time ] = _date.toLocaleString('fi-FI').split(' klo ')
		const [ day, month, year ] = fDate.split('.')
		const [ hour, minute, second ] = time.split('.')
		this.day = day
		this.month = month
		this.year = year
		this.hour = hour
		this.minute = minute
		this.second = second
		this.millisecond = _date.getMilliseconds()
		this.weekday = ["Su","Ma","Ti","Ke","To","Pe","La"][_date.getDay()] || ""
	}

	format(str = '[d].[m].[y]') {
		let formatted = str.replace(/[y]/g, this.year)
		formatted = formatted.replace(/[m]/g, this.month)
		formatted = formatted.replace(/[d]/g, this.day)
		formatted = formatted.replace(/[H]/g, this.hour)
		formatted = formatted.replace(/[M]/g, this.minute)
		formatted = formatted.replace(/[S]/g, this.second)
		formatted = formatted.replace(/[MS]/g, this.millisecond)
		return formatted
	}

}