使用javascript

时间:2016-04-13 03:14:08

标签: javascript

如何在JavaScript中将 201604130630 此数字转换为日期时间格式?

我正在使用XML文件,这些数据来自XML。我想转换成日期格式。请帮忙。

3 个答案:

答案 0 :(得分:0)

您可以通过拆分字符串

从字符串创建日期
import UIKit

class DetailViewController: UIViewController {

    @IBOutlet weak var detailDescriptionLabel: UILabel!

@IBOutlet var percentageJewsKilled: UILabel!
@IBOutlet var jewsKilled: UILabel!
let masterViewController = MasterViewController()

var detailItemDeath: AnyObject? {
    didSet {
        // Update the view.
        self.configureView()
    }
}
var detailItemCountry: AnyObject? {
    didSet {
        // Update the view.
        self.configureView()

    }
}

func configureView() {
    // Update the user interface for the detail item.
    if let detail = self.detailItemDeath {
        if let label = self.detailDescriptionLabel {
            label.text = detailItemDeath?.name
        }
        if let label = self.jewsKilled {
            label.text = "\(detailItemDeath?.jewishDeathCount)"
        }
    }
}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.configureView()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

然后您可以使用var input = '201604130630'; var year = input.slice(0, 4); // Date object uses month 0 index based var month = parseInt(input.slice(4, 6)) + 1; var day = input.slice(6, 8); var hour = input.slice(8, 10); var minute = input.slice(10, 12); var d = new Date(year, month, day, hour, minute); 方法连接值或格式化它:

答案 1 :(得分:0)

var str = "201604130630";
var year = str.slice(0, 4);
var month = parseInt(str.slice(4, 6))-1;
var day = str.slice(6, 8);
var hour = str.slice(8, 10);
var minute = str.slice(10, 12);
var d = new Date(year, month, day, hour, minute)
alert(d);

答案 2 :(得分:0)

var d = "201604130630";
var s = d.substring(0, 4) + '/' + d.substring(4, 6) + '/' + d.substring(6, 8) + ' ' + d.substring(8, 10) + ':' + d.substring(10, 12);
alert(s);