-1

Gabriele Petrioli please read my question before you close it.

my reports in SQL are daily, and INT as Date is perfect, two years ago I test Azure CosmosDB and I was able to convert by using JavaScript I did it by using UDF I just needed now for a project and bad luck, can't believe so many people are involved to downplay questions and administrating without programming skills and "managing", I will do it again I will not give up but I need the code today. I will post the solution if the managers did not close my question again.

here the original question:

I need to convert INT date produced by SQL into a Date using javascript.

I know this is possible I did it before I have a terrible day today and the time is clicking.

Here is the process: I convert the date to INT using SQL

select 'cast(getdate() as  int)'
select cast(getdate()  as  int)
-- today 43811

I need to bring it back using Javascript. No Jquery or else.

--more exaples
select 'cast(40702 as  smalldatetime)'
select cast(40702 as  smalldatetime)
--output 2011-06-10 00:00:00


select 'cast(getdate() as  float)'
select cast(getdate()  as  float)
-- output 43810.6597960262

Again I need to use JavaScript
convert2date(40702) --> 2011-06-10

convert2date(43811) --> 2019-12-14

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
Valentin Petkov
  • 1,570
  • 18
  • 23
  • 3
    why not get an iso date and use this for getting an instance? – Nina Scholz Dec 13 '19 at 21:26
  • @Valentin Maybe this [link](https://www.hashbangcode.com/article/convert-date-timestamp-javascript) might help you – Deleted Dec 13 '19 at 21:39
  • 3
    @Deleted no, this is for converting a JavaScript date *into* a Unix timestamp. OP wants to get a *SQL timestamp* and turn it into a JavaScript date. It's the opposite process and it's it's converting from a different time format. – VLAZ Dec 14 '19 at 09:06
  • Are you aware that if you do not cast it as an integer, you will have the format that you want? If this is for a homework, or you do not have actual control over the sql, please say so, that will limit the amount of suggestions you receive, because right now, the proper way to solve this would be not to cast as integer in the first place. Otherwise, maybe look at [date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date), the javascript built-in function, but it does not seem to be able to handle number of days. – Félix Adriyel Gagnon-Grenier Dec 14 '19 at 14:20
  • my reports in SQL are daily, and INT as Date is perfect, two years ago I test Azure CosmosDB and I was able to convert by using JavaScript I did it by using UDF I just needed now for a project and bad luck, can't believe so many people are involved to downplay questions and administrating without programming skills, I will do it again I will not give up but I need the code today. – Valentin Petkov Dec 14 '19 at 15:28

1 Answers1

1

What you have is a day count from 1900-01-01 and you need to add the given days.

function convert2date(days) {
    const
        isLeapYear = year => (!(year % 4) && !!(year % 100)) || !(year % 400),
        daysInMonth = (month, year) => [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1] + isLeapYear(year);


    var date = [1900, 1, 1],
        y = isLeapYear(date[0]) + 365,
        d;
    
    while (days >= y) {
        date[0]++;
        days -= y;
        y = isLeapYear(date[0]) + 365;
    }
    
    d = daysInMonth(date[1], date[0]);
    while (days >= d) {
        date[1]++;
        days -= d;
        d = daysInMonth(date[1], date[0]);
    }
    date[2] += days;
    
    return date.map((v, i) => v.toString().padStart(i ? 2 : 4, 0)).join('-');
}

console.log(convert2date(40702)); // 2011-06-10
console.log(convert2date(43811)); // 2019-12-14
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392