Created
June 9, 2019 02:08
-
-
Save MetaiR/371076b763db5f87cdadb255749617b5 to your computer and use it in GitHub Desktop.
Jalali date converter (needs jalali-moment library)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as moment from 'jalali-moment'; | |
export class DateHelper { | |
static monthsNames = [ | |
'فروردین', | |
'اردیبهشت', | |
'خرداد', | |
'تیر', | |
'مرداد', | |
'شهریور', | |
'مهر', | |
'آبان', | |
'آذر', | |
'دی', | |
'بهمن', | |
'اسفند' | |
]; | |
static convert(value: Date | string, withTime: boolean = true): string { | |
let str: string; | |
let result: string; | |
if (!value || value.toString().length === 0) { | |
value = new Date(); | |
} | |
if (typeof (value) !== 'string') { | |
str = `${value.getFullYear()}-${value.getMonth() + 1}-${value.getDate()}`; | |
if (withTime) { | |
str += ` ${value.getHours()}:${value.getMinutes()}:${value.getSeconds()}`; | |
} | |
} else { | |
str = value; | |
} | |
if (withTime) { | |
result = this.fullConvert(str); | |
} else { | |
result = this.halfConvert(str); | |
} | |
return result; | |
} | |
static convertMonthAndYearForCalendar(value: Date | string) { | |
let str: string; | |
let result: string; | |
if (value) { | |
if (typeof (value) !== 'string') { | |
str = `${value.getFullYear()}-${value.getMonth() + 1}-${value.getDate()}`; | |
} | |
const momentDate = moment(str, 'YYYY-MM-DD'); | |
result = momentDate.locale('fa').format('MMM jYYYY'); | |
} | |
return result; | |
} | |
private static fullConvert(str: string): string { | |
const momentDate = moment(str, 'YYYY-MM-DD HH:mm:ss'); | |
return momentDate.locale('fa').format('dddd، DD MMM jYYYY HH:mm:ss'); | |
} | |
private static halfConvert(str: string): string { | |
const momentDate = moment(str, 'YYYY-MM-DD'); | |
return momentDate.locale('fa').format('dddd، DD MMM jYYYY'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment