<p>I'm try to get the &quot;AGE&quot; column value from kubernetes api as in the command output</p>
<pre><code>&gt; kubectl get po
NAME                                        READY   STATUS    RESTARTS   AGE
ingress-nginx-controller-56c75d774d-d7zx8   1/1     Running   1          29d
nfs-client-provisioner-5cd984898b-s8fzh     1/1     Running   1          13d
</code></pre>
<pre><code>func getVolumes() VolumesOutput {

    userNamespace := &quot;test&quot;
    listOptions := metav1.ListOptions{
        Limit: 100,
    }

    volumeList, err := ClientSet.CoreV1().PersistentVolumeClaims(userNamespace).List(listOptions)

    if err != nil {
        // handle err
    }
    volumeItems := volumeList.Items

    volumes := make([]Volume, 0)
    if len(volumeItems) &gt; 0 {
        for _, volumeItem := range volumeItems {
            quant := volumeItem.Spec.Resources.Requests[v1.ResourceStorage]
            volume := Volume{
                Name: volumeItem.Name,
                Capacity: quant.String(),
                Age:      volumeItem.ObjectMeta.CreationTimestamp.String(),
                UserID:   UserID,
            }
            volumes = append(volumes, volume)

        }

    } else {
        fmt.Println(&quot;no volumes found&quot;)
    }
    log.Println(volumes)

    vol := VolumesOutput{
        Volumes: volumes,
    }

    return vol
}
</code></pre>
<p>So I can get the CreationTimestamp in</p>
<pre><code>Age:      volumeItem.ObjectMeta.CreationTimestamp.String(),
</code></pre>
<p>I tried to find the code that does the conversion from time to AGE couldn't find it in kubernetes github repo..</p>
<p>I like to convert it as the same as in kubectl does. So please give me an example and also point to the correct line that does it on repo.</p>

cdalar
<p>As the <a href="https://pkg.go.dev/k8s.io/apimachinery/pkg/apis/meta/v1#ObjectMeta" rel="nofollow noreferrer"><code>ObjectMeta.CreationTimestamp</code></a> is of  type <a href="https://golang.org/pkg/time/#Time" rel="nofollow noreferrer"><code>time.Time</code></a> , you can simply use <a href="https://golang.org/pkg/time/#Time.Sub" rel="nofollow noreferrer"><code>Time.Sub()</code></a> to get the difference between the current time and the creation timestamp which will you give you the age of the resource.</p>
<p>You can use <code>time.Now()</code> to get the current timestamp.</p>
<pre><code>package main

import (
    &quot;fmt&quot;
    &quot;time&quot;
)

func main() {

    currentTime := time.Now()
    creationTime := currentTime.Add(time.Hour * 12)
    
    diff := creationTime.Sub(currentTime)
    fmt.Println(&quot;Age:&quot;, diff)
}
</code></pre>
<p>Output:</p>
<pre><code>Age: 12h0m0s
</code></pre>
<p>Note: The above is used for example purpose since the creation time would be generally before the current time.</p>
