Getting A Build Date And Time In Xamarin Android And iOS (Release Mode)

Introduction

If we want to get the Build date and time in Xamarin Android & iOS in Release mode then this article is for you. Let us go one by one.

Requirements

  1. This article's source code is prepared using Visual Studio. And it is better to install the latest Visual Studio updates from here.
  2. This article is prepared on a MAC machine.
  3. This sample project is Xamarin.Forms PCL project.
  4. This sample app is targeted for Android, iOS. and, tested for Android & iOS.

Description

Each time when we generate apk or bundle we will get the build date and time and mainly we are able to get the build in Debug mode. But in release mode we need to do small steps. which I was found by me. and I will share how to get the build date and time in both Xamarin.Android & Xamarin.iOS. Let's go.

Step 1 - Shared project

If your app name is called MyApp then open the MyApp.csproj and paste below the TargetFramework propertyGroup

<PropertyGroup>
    <SourceRevisionId>build$([System.DateTime]::UtcNow.ToString("yyyyMMddHHmmssff"))</SourceRevisionId>
</PropertyGroup>

Note

  1. Using the .NET SDK, you can use an MSBuild property to define the value of the AssemblyInformationalVersionAttribute. So, you can inject the value of DateTime.UtcNow in the value of this attribute and then parse it at runtime to get the build date.
  2. The value of SourceRevisionId is added to the metadata section of the version (after the +).

Step 2

Create a new Class & Create a new property called BuildDate and follow the code below.

public static string BuildDate {
    get {
        TimeZoneInfo easternZone;
        string ret = "";
        try {
            var DateandTime = GetBuildDate(Assembly.GetExecutingAssembly());
            try {
                easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
            } catch (TimeZoneNotFoundException) {
                easternZone = TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
            }
            var EstDateTime = TimeZoneInfo.ConvertTimeFromUtc(DateandTime, easternZone); //You will get the date and time of the build
            //ret = string.Format("{0:dd-MM-yyyy hh:mm:ss.ff EST}", EstDateTime); Convert into EST Time Format
        } catch (Exception Ex) {
            Logger.Error(Ex);
        }
        return ret;
    }
}

OUTPUT

Above I got the Build and time and I converted it into EST timezone format. You can convert it to whatever timezone you want and this will work for both Android & iOS.

Hope you all get the Build Date and Time in Release mode.

Note
Any doubt please feel free to ask in comment. 

If you want to get the date of build of a .NET assembly at runtime. Please refer here

Thanks for reading & keep learning.


Recommended Free Ebook
Similar Articles