1

i have an epoch time: x = 1383483902000

I am trying to get a date like: dd-mm-yyyy and want to do this without additional library.

I have tried several ways and my last method end up like :

var date = new Date(Math.round(Number(x)));

but i get an ugly thing like: sun nov 03 2013 14:05:02 GMT+01:00

Terradon
  • 883
  • 2
  • 13
  • 33
  • 1
    Duplicate of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date please do your research. – PeeHaa Nov 04 '13 at 19:30
  • Since you don't want a library, read: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – user2864740 Nov 04 '13 at 19:30
  • 1
    see http://stackoverflow.com/a/8016205/649388 . it is similar to what you are looking for. – waqas Nov 04 '13 at 19:32
  • Thank you all for yor fast comments, but i already have read those links in the last few days. I really don't see the clue for how to convert epoch to dd-mm-yyyy (pitty of the downvote, but i already took a weekdosis of aspirine to find an understandable solution) – Terradon Nov 04 '13 at 19:38
  • Thank you all, i got my solution within 10 minutes, while i could not find it for days. I also learned soemthing about the date object. However, i will never understand the downvote for my question. I asked here, because i got lost in my 2 days search. – Terradon Nov 04 '13 at 19:58
  • I didn't down vote, but a question like this will get downvotes because you included only your last example of what you tried, plus there are a number of "Related" links on the right which suggests you didn't try searching SO for the subject of your own question. It's best to try and demonstrate what you've tried and what results you're rejecting because then respondents don't waste their time giving an answer you have already discarded. – kfsone Nov 05 '13 at 08:18
  • kfsone, thanks for the explanation. Now the downvote does make sense to me. in future i will add my problems'history:) – Terradon Nov 06 '13 at 14:14

3 Answers3

7

Use your date object to extract/format the parts you want:

var formattedDate = date.getUTCDate() + '-' + (date.getUTCMonth() + 1)+ '-' + date.getUTCFullYear()
Ben
  • 123
  • 4
2

The example below uses the UTC methods of the date object as you are dealing with epoch time (which is milliseconds since epoch in UTC):

var formatDate = function formatDate(date) {            // function for reusability
        var d = date.getUTCDate().toString(),           // getUTCDate() returns 1 - 31
            m = (date.getUTCMonth() + 1).toString(),    // getUTCMonth() returns 0 - 11
            y = date.getUTCFullYear().toString(),       // getUTCFullYear() returns a 4-digit year
            formatted = '';
        if (d.length === 1) {                           // pad to two digits if needed
            d = '0' + d;
        }
        if (m.length === 1) {                           // pad to two digits if needed
            m = '0' + m;
        }
        formatted = d + '-' + m + '-' + y;              // concatenate for output
        return formatted;
    },
    x = 1383483902000,                                  // sample time in ms since epoch
    d = new Date(x),                                    // convert to date object
    f = formatDate(d);                                  // pass to formatDate function to get dd-mm-yyyy
console.log(f);                                         // log output to console for testing

You can run this in the browser console as-is.

pete
  • 24,141
  • 4
  • 37
  • 51
  • Thank you too, i have chosen the shorter answer from Ben as accepted, but will try to create some date format functions from your example. Thats is why i upvoted your answer as usefull. Thans again! – Terradon Nov 04 '13 at 19:53
0
function formatDate(value: any): any{
    
    let date = new Date(Math.round(Number(value)));
    let day = ("0" + date.getDate()).slice(-2);
    let month = ("0" + (date.getMonth() + 1)).slice(-2);
    let formatDate = date.getFullYear()+"-"+(month)+"-"+(day) ;

    return formatDate;
}