Skip to main content

Convert date from BS to AD

Short answer

Count the days between the given BS date and a reference (2080/01/01 BS = 2023/04/14 AD) using a table of BS month lengths, then add those days to the reference AD date. Example: 2081/06/15 BS converts to 2024/10/01 AD.

Write a C program to convert date in B.S. to A.D. using structure. (Pass the structure to the function that returns the result to convert the date (in format YY/MM/DD) in B.S. to A.D. / use the date difference of current date.)

Idea (date difference method). A BS date is converted by counting the days between a known reference date and the given date, then adding that many days to the matching AD reference date. The reference used here is 2080/01/01 BS = 2023/04/14 AD. The number of days in a BS month is not fixed (29 to 32) and changes from year to year, so a table of month lengths is needed. To keep the program short, the table below covers only 2080 and 2081 BS; a full converter needs the table for every year. The program therefore checks the input and rejects any date outside the table.

Steps:

  1. A structure date holds year, month and day.
  2. main() reads the BS date and checks that the year is in the table and that the month and day are valid.
  3. bsToAd() receives the date structure, counts the days from 2080/01/01 BS using the table, and adds them one by one to 2023/04/14 AD (taking care of month ends and leap years).
  4. The function returns the AD date as a structure, which main() displays.
#include <stdio.h>

struct date {
    int year, month, day;
};

/* days in each BS month; table available for 2080 and 2081 only */
int bsDays[2][12] = {
    {31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30},   /* 2080 */
    {31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30}    /* 2081 */
};

int adDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int isLeap(int y)
{
    return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
}

/* 2080/01/01 BS = 2023/04/14 AD */
struct date bsToAd(struct date bs)
{
    struct date ad = {2023, 4, 14};
    int diff = 0;                               /* days since 2080/01/01 */

    for (int y = 2080; y < bs.year; y++)
        for (int m = 0; m < 12; m++)
            diff += bsDays[y - 2080][m];
    for (int m = 0; m < bs.month - 1; m++)
        diff += bsDays[bs.year - 2080][m];
    diff += bs.day - 1;

    while (diff > 0) {                          /* add days to the AD date */
        int dim = adDays[ad.month - 1] + (ad.month == 2 && isLeap(ad.year));
        if (ad.day < dim)
            ad.day++;
        else {
            ad.day = 1;
            if (++ad.month > 12) {
                ad.month = 1;
                ad.year++;
            }
        }
        diff--;
    }
    return ad;
}

int main(void)
{
    struct date bs, ad;
    printf("Enter BS date (YYYY/MM/DD, 2080-2081): ");
    if (scanf("%d/%d/%d", &bs.year, &bs.month, &bs.day) != 3
        || bs.year < 2080 || bs.year > 2081
        || bs.month < 1 || bs.month > 12
        || bs.day < 1 || bs.day > bsDays[bs.year - 2080][bs.month - 1]) {
        printf("Invalid date or year outside the table\n");
        return 1;
    }
    ad = bsToAd(bs);
    printf("AD date = %04d/%02d/%02d\n", ad.year, ad.month, ad.day);
    return 0;
}

Sample run

Input: 2081/06/15

Enter BS date (YYYY/MM/DD, 2080-2081): AD date = 2024/10/01

2081/06/15 BS (15 Ashwin) corresponds to 1 October 2024 AD. An input such as 2082/01/01 prints Invalid date or year outside the table; to support it, add that year's row to bsDays.

(The quick approximate rule used in some textbooks is: AD year = BS year − 57 from 1 Baisakh (about 13/14 April) to the end of December, and BS year − 56 from January to mid-April. It does not give the exact day.)

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 8 questions →