duanjue7745 2015-01-06 10:37
浏览 58

管理angularjs之间的时间格式 - php - mysql

i have trouble to manage time format between angularjs(javascript) - php - mysql . I stored time like 24:00:00, 12:00:00,01:30:00,05:34:00 from angular to mysql via php. Here i m using mysql's time type. While receiving the time via php from mysql's i got as string , but javascript ill understand date object then only i can able to show as time to views . Is any idea to handle this situation? I am used some time picker but those are not make my result better , I added the list of used plugins , please refer and also you my guide me to handle this .

Demo link

github link

here this author used as directive but i ill initialize model with above mentioned time format , i get invalidate date as result in console while pick the date , after submission the nulled value only passed as model value to server.

  • 写回答

1条回答 默认 最新

  • dongzhangji4824 2015-01-06 11:00
    关注

    Sometimes is easier to work with Date.prototype.getTime()

    var birthday = new Date(1994, 12, 10);
    var time = birthday.getTime(); // 789696000000
    var date2 = new Date(time); // Same as birthday 
    

    Then in the back end you can transform it into date with a simple helper. I have some C# (Migrate it to PHP or should be easy) code that do the job:

    public class JsDateHelper : IJsDateHelper
    {
        public DateTime FromUnixTime(double unixTime)
        {
            var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            return epoch.AddMilliseconds(unixTime);
        }
    
        public long ToUnixTime(DateTime date)
        {
            var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            return Convert.ToInt64((date - epoch).TotalMilliseconds);
        }
    }
    

    By doing it this way you will store in database the number 789696000000 and pass it to angular in angular you will transform it into an actual date using new Date(789696000000) I think this way reduces complexity because you only have to worry about the format when you are going display the date.

    Hope it helps!

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘