Skip to content

Instantly share code, notes, and snippets.

@emoacht
Created May 31, 2026 10:11
Show Gist options
  • Select an option

  • Save emoacht/b2e47fc7a16e70e7852a777d583d6379 to your computer and use it in GitHub Desktop.

Select an option

Save emoacht/b2e47fc7a16e70e7852a777d583d6379 to your computer and use it in GitHub Desktop.
Read Exif DateTimeOriginal and SubSecTimeOriginal of JPG file and convert its centiseconds in Base36. This is intended for JPG files by Nikon camera.
using System.Globalization;
using System.Text;
internal static class ExifDate
{
public static string? GetBase36UnixCentiseconds(string filePath)
{
if (TryRead(filePath, out DateTimeOffset dateTime, out int subSecTime))
{
var centiseconds = dateTime.ToUnixTimeSeconds() * 100 + subSecTime; // in centiseconds
Console.WriteLine(centiseconds);
return Convert(centiseconds);
}
return null;
static string Convert(long value)
{
const string array = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (value == 0)
return "0";
var buffer = new StringBuilder();
while (value > 0)
{
buffer.Insert(0, array[(int)(value % 36)]);
value /= 36;
}
return buffer.ToString();
}
}
public static bool TryRead(string filePath, out DateTimeOffset dateTime, out int subSecTime)
{
dateTime = default;
subSecTime = 0;
if (!File.Exists(filePath))
return false;
using var image = System.Drawing.Image.FromFile(filePath);
string? GetValue(int id)
{
try
{
var prop = image.GetPropertyItem(id);
if (prop?.Value is null)
return null;
return Encoding.ASCII.GetString(prop.Value).TrimEnd('\0');
}
catch (ArgumentException)
{
return null;
}
}
var dateTimeOriginal = GetValue(36867);
if (!DateTimeOffset.TryParseExact(dateTimeOriginal, "yyyy:MM:dd HH:mm:ss", null, DateTimeStyles.None, out dateTime))
return false;
var subSecTimeOriginal = GetValue(37521);
if (!int.TryParse(subSecTimeOriginal, out subSecTime) || (subSecTime >= 100))
return false;
return true;
}
}
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Get & convert Exif date");
foreach (var filePath in Directory.GetFiles(args[0], "*.JPG", SearchOption.AllDirectories))
{
var converted = ExifDate.GetBase36UnixCentiseconds(filePath);
Console.WriteLine($"{filePath} -> {converted}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment