Converting .NET DateTime to JSON [duplicate]

What is returned is milliseconds since epoch. You could do: var d = new Date(); d.setTime(1245398693390); document.write(d); On how to format the date exactly as you want, see full Date reference at http://www.w3schools.com/jsref/jsref_obj_date.asp You could strip the non-digits by either parsing the integer (as suggested here): var date = new Date(parseInt(jsonDate.substr(6))); Or applying the following … Read more

Changing the formatting of a datetime axis in matplotlib

import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib.dates as mdates # sample data N = 30 drange = pd.date_range(“2014-01″, periods=N, freq=”MS”) np.random.seed(365) # for a reproducible example of values values = {‘values’:np.random.randint(1,20,size=N)} df = pd.DataFrame(values, index=drange) fig, ax = plt.subplots() ax.plot(df.index, df.values) ax.set_xticks(df.index) # use formatters to specify major … Read more

SimpleDateFormat parsing date with ‘Z’ literal [duplicate]

Java doesn’t parse ISO dates correctly. Similar to McKenzie’s answer. Just fix the Z before parsing. Code String string = “2013-03-05T18:05:05.000Z”; String defaultTimezone = TimeZone.getDefault().getID(); Date date = (new SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss.SSSZ”)).parse(string.replaceAll(“Z$”, “+0000”)); System.out.println(“string: ” + string); System.out.println(“defaultTimezone: ” + defaultTimezone); System.out.println(“date: ” + (new SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss.SSSZ”)).format(date)); Result string: 2013-03-05T18:05:05.000Z defaultTimezone: America/New_York date: 2013-03-05T13:05:05.000-0500

How to construct a timedelta object from a simple string

To me the most elegant solution, without having to resort to external libraries such as dateutil or manually parsing the input, is to use datetime’s powerful strptime string parsing method. from datetime import datetime, timedelta # we specify the input and the format… t = datetime.strptime(“05:20:25″,”%H:%M:%S”) # …and use datetime’s hour, min and sec properties … Read more

How to make a timezone aware datetime object

In general, to make a naive datetime timezone-aware, use the localize method: import datetime import pytz unaware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0) aware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0, pytz.UTC) now_aware = pytz.utc.localize(unaware) assert aware == now_aware For the UTC timezone, it is not really necessary to use localize since … Read more

How to group by week in MySQL?

You can use both YEAR(timestamp) and WEEK(timestamp), and use both of the these expressions in the SELECT and the GROUP BY clause. Not overly elegant, but functional… And of course you can combine these two date parts in a single expression as well, i.e. something like SELECT CONCAT(YEAR(timestamp), “https://stackoverflow.com/”, WEEK(timestamp)), etc… FROM … WHERE .. … Read more