如何拆分日期字符串并在不同的变量中获取结果字符串?

时间:2018-11-27 12:31:18

标签: javascript

我有变数 var dataValue = '10/2019' 我想在不同的变量中获得值2019和10。所以我有var dataA = 10, and var dataB = 2019

如何解决这个问题?

3 个答案:

答案 0 :(得分:5)

如果您想要合理的变量命名和变量的默认值,建议这样做:

const date = '10/2019';
const [ month = 'defaultMonth', year = 'defaultYear' ] = date.split('/');

这将确保您不会得到未定义的信息(这会导致RN中出现RSoD)。

答案 1 :(得分:0)

使用js中的split函数,如下所示:

#include <vector>
#include <algorithm>    

class Polygon3D
{
public:
    Polygon3D(float depth) : _depth(depth) {}

    bool operator < (const Polygon3D& rhs) const
    {
        return (this->GetDepth() < rhs.GetDepth());
    }

    float GetDepth() const
    {
        return _depth;
    }

private:
    float _depth;
};

class Model
{
public:
    void InsertPolygon(Polygon3D polygon)
    {
        _polygons.push_back(polygon);
    }

    void Sort()
    {
        std::sort(_polygons.begin(), _polygons.end());
    }
private:
    std::vector<Polygon3D> _polygons;
};


int main()
{
    Model model;

    for (int i = 0; i < 10; ++i)
        model.InsertPolygon(Polygon3D(std::rand()));

    model.Sort();

    return 0;
}

答案 2 :(得分:0)

使用以下

var actual_value = '10/2019' 
var data = actual_value.split('/')

var data1 = data[0] // returns "10"
var data2 = data[1] // returns "2019"