最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

date - How to use JodaTime in JavaScript (AngularJS) - Stack Overflow

programmeradmin2浏览0评论

I'm using DateTime to create Date objects and sending them to UI as JSON. How can I use that in JavaScript (Angular JS) and convert it back and forth.

Example: If I want to get the user update the time, I should be able to get the Date/Time from a form submission and send it back to the Server in json.

Here is my current DateTime json output that the client recives

"date": {
                "year": 2013,
                "era": 1,
                "dayOfMonth": 3,
                "dayOfWeek": 7,
                "dayOfYear": 34,
                "millisOfDay": 3660000,
                "centuryOfEra": 20,
                "yearOfCentury": 13,
                "monthOfYear": 2,
                "weekOfWeekyear": 5,
                "millisOfSecond": 0,
                "hourOfDay": 1,
                "yearOfEra": 2013,
                "weekyear": 2013,
                "secondOfMinute": 0,
                "secondOfDay": 3660,
                "minuteOfHour": 1,
                "minuteOfDay": 61,
                "zone": {
                    "fixed": true,
                    "id": "UTC"
                },
                "millis": 1359853260000,
                "chronology": {
                    "zone": {
                        "fixed": true,
                        "id": "UTC"
                    }
                },
                "beforeNow": true,
                "afterNow": false,
                "equalNow": false
            }

I'm using DateTime to create Date objects and sending them to UI as JSON. How can I use that in JavaScript (Angular JS) and convert it back and forth.

Example: If I want to get the user update the time, I should be able to get the Date/Time from a form submission and send it back to the Server in json.

Here is my current DateTime json output that the client recives

"date": {
                "year": 2013,
                "era": 1,
                "dayOfMonth": 3,
                "dayOfWeek": 7,
                "dayOfYear": 34,
                "millisOfDay": 3660000,
                "centuryOfEra": 20,
                "yearOfCentury": 13,
                "monthOfYear": 2,
                "weekOfWeekyear": 5,
                "millisOfSecond": 0,
                "hourOfDay": 1,
                "yearOfEra": 2013,
                "weekyear": 2013,
                "secondOfMinute": 0,
                "secondOfDay": 3660,
                "minuteOfHour": 1,
                "minuteOfDay": 61,
                "zone": {
                    "fixed": true,
                    "id": "UTC"
                },
                "millis": 1359853260000,
                "chronology": {
                    "zone": {
                        "fixed": true,
                        "id": "UTC"
                    }
                },
                "beforeNow": true,
                "afterNow": false,
                "equalNow": false
            }
Share Improve this question edited Aug 8, 2013 at 13:58 Gray 117k24 gold badges302 silver badges359 bronze badges asked May 27, 2013 at 16:45 user6123723user6123723 11.2k19 gold badges72 silver badges111 bronze badges 1
  • 1 you don't have a timestamp ? – Ven Commented May 27, 2013 at 16:49
Add a ment  | 

2 Answers 2

Reset to default 4

I will take the mentor's question and recast it as an answer. Make sure your server outputs the date/time in a more simplified view such as a timestamp, just the milliseconds and either assume UTC or send that as well. I have solved it given your current message above, but it really is a waste, message should be more simple.

new Date(date.millis); gives you a javascript object with the time and date of Sat Feb 02 2013 19:01:00 GMT-0600 (Central Standard Time)

Central in my case.

Other post handles the UTC questions new Date().setUTCSeconds(date.millis); Convert UTC Epoch to local date with javascript

I would strongly suggest using ISO 8601 for your times. It includes the date and time, as per a timestamp, but also a timezone so that you have the additional information required to store and display it in the correct format for the user who submitted it. This is useful information to keep around if you have users in multiple timezones.

发布评论

评论列表(0)

  1. 暂无评论