打字稿日期格式

时间:2017-10-03 16:23:56

标签: typescript

我正在使用角度2/4  如何将日期格式化为此 2017-10-03T14:51:06.078Z

这是我的代码:

  public today: number = Date.now();
 console.log("today time " +this.today);

我尝试添加

this.today.toLocaleString()

似乎无法正常工作

2 个答案:

答案 0 :(得分:3)

您应该使用toISOString方法:

this.today.toISOString();

但您需要将类型从数字更改为日期:

namespace Testing {
    export class Test {
        public today: Date;

        constructor() {
            this.today = new Date();
            console.log(this.today.toISOString());
        }
    }
}

可以找到更多信息here

答案 1 :(得分:3)

你可以这样做

in_file_path

您可以查看控制台日志,

import json

in_file_path='path/to/file.json' # Change me!

with open(in_file_path,'r') as in_json_file:

    # Read the file and convert it to a dictionary
    json_obj_list = json.load(in_json_file)

    for json_obj in json_obj_list:
        filename=json_obj['_id']+'.json'

        with open(filename, 'w') as out_json_file:
            # Save each obj to their respective filepath
            # with pretty formatting thanks to `indent=4`
            json.dump(json_obj, out_json_file, indent=4)

输出

import requests ; from bs4 import BeautifulSoup
from urllib.request import urlopen
from zipfile import ZipFile

base_link = "http://www.diva-gis.org/datadown"

def download_file(link):
    payload = {'cnt':'AFG_Afghanistan','thm':'adm#Administrative areas (GADM)','OK':'OK','_submit_check':'1'}

    headers={
        "Content-Type":"application/x-www-form-urlencoded",
        "User-Agent":"Mozilla/5.0"
    }

    page = requests.post(link, data=payload, headers=headers)
    soup = BeautifulSoup(page.text,"lxml")
    for item in soup.select(".field-item a"):
        zipresp = urlopen(item['href'])
        tempzip = open(r"C:\Users\ar\Desktop\mth.zip", "wb")  #change the address according to your need
        tempzip.write(zipresp.read())
        tempzip.close()

download_file(base_link)

阐释: “this.dateToday”是任何类型的变量,它将根据您的需要存储日期。

您需要获得单独的年,月,日期和时间,并将它们连接在一起。 您可以在线或在IDE中查看更多日期方法。