Translate

Friday, April 8, 2022

Golang - Working with active directory date attributes

Some simple functions I wrote to convert AD date attributes to dates as stings.


//Converts a string passed in with the millisecond count to the date in the form yyyy-MM-dd HH:mm:ss as a string
func convertStringToDate64(strDate string) (string, error) {
	uD, err := strconv.ParseInt(strDate, 0, 64)
	if err == nil {
		return time.Unix((uD/(10000000))-11644473600, 0).Format("2006-01-02 15:04:05"), nil
	}
	return "", nil
}

//Converts a string passed in the form yyyyMMddHHmmss.0Z to the date in the form yyyy-MM-dd HH:mm:ss as a string
func convertStringToDateZ(strDate string) (string, error) {
	uD, err := time.Parse("20060102150405", strings.Replace(strDate, ".0Z", "", 1))
	if err == nil {
		return uD.Format("2006-01-02 15:04:05"), nil
	}
	return "", nil
}

No comments:

Post a Comment

Thank you for commenting!