如何在swift中将字符串转换为日期

时间:2015-09-21 05:06:53

标签: swift string-to-datetime

我解析了代码" 1442351280"来自JSON,我希望将其转换为中等风格的swift日期。

2 个答案:

答案 0 :(得分:0)

您必须使用Foundation API

      <table align="center" width="600"> 
           <tr>
           <tr>
           <td bgcolor="#ffffff" valign="bottom" align="left"><a href="http://nrsadvisors.com/"><img border="0" src="http://www.nrsadvisors.com/email-images/mainlogo.jpg" alt="NRS Advisors"/></a></td>
           </tr>
           <tr>
                <td style="border-top-width:2px; border-top-style:solid; border-top-color: #3E7DBD;" bgcolor="#dbdbdb"><img border="0"  src="http://www.nrsadvisors.com/email-images/banner-hero-image.png" alt="bannerimg" width="550" /><a name="featuredstartup" style="height:0;margin:0;padding:0"></a></td>
              </tr>
           <td bgcolor="#dbdbdb" align="center"><table border="0" cellpadding="0" cellspacing="0" width="550" align="center">
           <tr>

           <td width="25%"><a href="#featuredstartup"><img src="uploads/<?php     echo $arr['activeimage'] ?>_blue.jpg" width="125px" height="134px" />
</a></td>
           <td width="25%"><a href="#healthcaredeals"><img src="uploads/<?php       echo $arr['activeimage'] ?>_grey.jpg" width="125px" height="134px" /></a></td>
           <td width="25%"><a href="#healthcarenews"><img src="uploads/<?php     echo $arr['activeimage'] ?>_grey.jpg" width="125px" height="134px" /></a></td>
           <td width="25%"><a href="#healthcaremarkets"><img src="uploads/<?php     echo $arr['activeimage'] ?>_grey.jpg" width="125px" height="134px" /></a></td>

           </tr>
           </table>
           </td>
           </tr>

            <?php   
                while ($row = mysql_fetch_array($result_news)) {
           ?>
           <table align="center" width="600"> 
           <tr>

           <span style="font-size:14px;line-height:17px;font-weight:normal;color:#333333">
           <td align="left">
           <span style="font-weight:bold">
           <?php echo $row['newsid'] ?>
           </span>
           </td>
           </span>
           </tr>
           <tr>
           <td>
            <span align='left' style='color:#3E7DBD;font-weight:bold'>
            <span style='font-size:14px;line-height:17px;font-weight:normal;color:#333333'><span style='font-weight:bold'>
            <?php echo $row['newstitle'] ?>
            -</span><br><br>
            </td>
            </tr>
            <tr>
            <td><?php echo $row['Description'] ?></td><br><br>
            </tr>          
            <tr>
            <td>
            <a href="<?php echo $row['urlofnews'] ?>" style='font-size:14px;line-height:17px;color:#3FC47B; text-decoration:none;'><?php echo $row['titleoflink'] ?></a></td><br><br>
            </tr>
            <tr>
            <td><?php echo $row['news_img'] ?></td>
            </tr>
            </table>

      <?php
             }
        ?>

答案 1 :(得分:0)

正如THIS帖中提到的那样。您从服务器获取时间间隔

以下是你的答案:

var string : String = "1442351280"

var timeinterval : NSTimeInterval = (string as NSString).doubleValue

var dateFromServer = NSDate(timeIntervalSince1970:timeinterval)
print(dateFromServer)   //"sep 16,2015, 2:38 AM"