0

I'm having a problem with the date format in javascript. I'm getting a date in seconds (in time_t format) from a database (ex. 1364565600) Now I want to convert this date to day, month, day (ex. Tuesday, March, 18th).

I hope this is possible.

 timestart: function (time_start) {
                ///////////////////////////////
                //////code for conversion//////
                return  time_start;
             }

Thanks in advance!

Bjorn Ravers
  • 1
  • 2
  • 3

3 Answers3

1

Multiply the seconds you're getting by 1000 and use a new Date() object, which takes milliseconds as a parameter (which is based on the same idea as time_t, which is seconds since epoch, but Date() is based on milliseconds):

 timestart: function (time_start) {

            return new Date(1000 * time_start);
         }

To get the Date string from it, use .toDateString(). There are a few other methods you could use to grab the date information and convert it to the types you want, you can find them here: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date

Rob G
  • 3,496
  • 1
  • 20
  • 29
  • Oops, forgot the 'new' operator, there it is now. – Rob G Mar 28 '13 at 04:12
  • I know Date gives back miliseconds, but what I'm doing now is creating a time in miliseconds, I want a date in Tuesday, March, 18th. How do I put the miliseconds one into that format? Thanks for the fast reply – Bjorn Ravers Mar 28 '13 at 04:20
  • Visit http://stackoverflow.com/questions/8262333/how-to-convert-seconds-into-date-and-time-format-in-java here you will find what you looking for. –  Mar 28 '13 at 04:26
  • James, that's for Java, not JavaScript. I updated my post to tell the original poster the way to get the date string. – Rob G Mar 28 '13 at 04:42
  • I can't get it to work, I'm kinda new to javascript. This is what I have. timestart: function (time_start) { var d = new Date(time_start).toDateString(); return d; } }) Sorry for the noobness, I'm learning. – Bjorn Ravers Mar 28 '13 at 06:25
0

For verbose date manipulation in JavaScript, it's easiest to use a library if your deployment/optimization requirements permit. I've found Moment JS to be a good solution.

metadept
  • 7,831
  • 2
  • 18
  • 25
  • I'm working with sencha touch 2.1 I don't know if it is possible. Is it possible to format the date to 2013/03/18 starting from milisec? Thanks for the reply – Bjorn Ravers Mar 28 '13 at 04:39
  • [This thread](http://stackoverflow.com/questions/10334893/what-is-the-proper-way-to-load-an-external-javascript-in-sencha-touch-2) seems to be what you're looking for if you want to use an external library with Sencha Touch. If you just need the date to be readable (rather than a specific format), I suggest you go with @Rob G's answer and use `.toDateString()` – metadept Mar 28 '13 at 05:19
  • Usually there is only a requirement for one or two date formats, it's pretty easy to write a formatting function that suit. – RobG Mar 28 '13 at 06:30
0

The time value in a javascript date object is milliseconds since 1970-01-01T00:00:00Z (note UTC). As Rob G said, you can pass that value to the Date constructor to get a date object, then use Date methods to get what you want.

If you have a UTC time value in seconds from the same epoch, multiply it by 1,000 to get milliseconds and pass to the date constructor:

var date = new Date(timeValue);

You can now get either local or UTC date and time strings from the object using methods like toString, toLocaleString, toISOString and so on. You can also use standard Date methods to get local time and date values, or UTC methods (e.g. getUTCFullYear) to get UTC values, to build a string of your own format.

RobG
  • 142,382
  • 31
  • 172
  • 209