Posts

Showing posts with the label Date

Compute Date Out Of Timestamp From Binance-API (Python)

Answer : You could use this: from datetime import datetime datetime.fromtimestamp(int("1518308894652")) But python says the year is out of range (understandably, considering it says it's 50087). So I suspect that serverTime is not a normal timestamp. But assuming the response that you got was the timestamp, so you don't need to do any other conversions other than turning the string into an int. Edit: Turns out the docs say "All time and timestamp related fields are in milliseconds." So just divide the response by 1000 and you'll be fine: datetime.fromtimestamp(int("1518308894652")/1000) . Source Your response is in milliseconds when datetime.fromtimestamp requires seconds. import datetime print(datetime.datetime.fromtimestamp(1518308894652/1000)) # 2018-02-10 19:28:14.652000

CakePHP Find Condition For A Query Between Two Dates

Answer : $conditions = array( 'conditions' => array( 'and' => array( array('Item.date_start <= ' => $date, 'Item.date_end >= ' => $date ), 'Item.title LIKE' => "%$title%", 'Item.status_id =' => '1' ))); Try the above code and ask if it not worked for you. Edit: As per @Aryan request, if we have to find users registered between 1 month: $start_date = '2013-05-26'; //should be in YYYY-MM-DD format $this->User->find('all', array('conditions' => array('User.reg_date BETWEEN '.$start_date.' AND DATE_ADD('.$start_date.', INTERVAL 30 DAY)'))); Here is CakePHP BETWEEN query example. I'm defining my arrays as variables, and then using those variables in my CakePHP find function call: // just return...

Adding Days To $Date In PHP

Answer : All you have to do is use days instead of day like this: <?php $Date = "2010-09-17"; echo date('Y-m-d', strtotime($Date. ' + 1 days')); echo date('Y-m-d', strtotime($Date. ' + 2 days')); ?> And it outputs correctly: 2010-09-18 2010-09-19 If you're using PHP 5.3, you can use a DateTime object and its add method: $Date1 = '2010-09-17'; $date = new DateTime($Date1); $date->add(new DateInterval('P1D')); // P1D means a period of 1 day $Date2 = $date->format('Y-m-d'); Take a look at the DateInterval constructor manual page to see how to construct other periods to add to your date (2 days would be 'P2D' , 3 would be 'P3D' , and so on). Without PHP 5.3, you should be able to use strtotime the way you did it (I've tested it and it works in both 5.1.6 and 5.2.10): $Date1 = '2010-09-17'; $Date2 = date('Y-m-d', strtotime($Date1 . " + 1 day...

Convert Integer (YYYYMMDD) To Date Format (mm/dd/yyyy) In Python

Answer : You can use datetime methods. from datetime import datetime a = '20160228' date = datetime.strptime(a, '%Y%m%d').strftime('%m/%d/%Y') Good Luck; Build a new column with applymap : import pandas as pd dates = [ 20160228, 20161231, 20160618, 20170123, 20151124, ] df = pd.DataFrame(data=list(enumerate(dates, start=1)), columns=['id','int_date']) df[['str_date']] = df[['int_date']].applymap(str).applymap(lambda s: "{}/{}/{}".format(s[4:6],s[6:], s[0:4])) print(df) Emits: $ python test.py id int_date str_date 0 1 20160228 02/28/2016 1 2 20161231 12/31/2016 2 3 20160618 06/18/2016 3 4 20170123 01/23/2017 4 5 20151124 11/24/2015 There is bound to be a better solution to this, but since you have zeroes instead of single-digit elements in your date (i.e. 06 instead of 6), why not just convert it to string and convert the subsections? using datetime would also get you the...

Converting ISO 8601-compliant String To Java.util.Date

Answer : Unfortunately, the time zone formats available to SimpleDateFormat (Java 6 and earlier) are not ISO 8601 compliant. SimpleDateFormat understands time zone strings like "GMT+01:00" or "+0100", the latter according to RFC # 822. Even if Java 7 added support for time zone descriptors according to ISO 8601, SimpleDateFormat is still not able to properly parse a complete date string, as it has no support for optional parts. Reformatting your input string using regexp is certainly one possibility, but the replacement rules are not as simple as in your question: Some time zones are not full hours off UTC, so the string does not necessarily end with ":00". ISO8601 allows only the number of hours to be included in the time zone, so "+01" is equivalent to "+01:00" ISO8601 allows the usage of "Z" to indicate UTC instead of "+00:00". The easier solution is possibly to use the data type converter in JAXB, since JAXB m...

Convert XMLGregorianCalendar To Date I.e "MM/DD/YYYY Hh:mm:ss AM"

Answer : You can do this to return a Date : calendar.toGregorianCalendar().getTime() I found that code from this tutorial. From there, you can use a SimpleDateFormat to turn it into a string in the format you want. But, if you're using JDBC to save the date in the database, you probably can pass in the Date directly with this method: preparedStatement.setDate(colNum, myDate); Here is more clear answer: Get instance of Date from XMLGregorianCalendar instance: Date date = xmlCalendar.toGregorianCalendar().getTime(); I found that code from Convert XMLGregorianCalendar to Date in Java Format that Date instance with format "MM/dd/yyyy hh:mm:ss a", you will get MM/DD/YYYY hh:mm:ss AM format DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a"); String formattedDate = formatter.format(date) From Convert Date to String in Java For inserting database you would do what Daniel suggested If you want to insert your date on a database I would first do w...

Convert Unix Timestamp To Human-readable Time

Answer : If the line starts with the unix timestamp, then this should do it: perl -pe 's/^(\d+)/localtime $1/e' inputfilename perl -p invokes a loop over the expression passed with -e which is executed for each line of the input, and prints the buffer at the end of the loop. The expression uses the substition command s/// to match and capture a sequence of digits at the beginning of each line, and replace it with the local time representation of those digits interpreted as a unix timestamp. /e indicates that the replacement pattern is to be evaluated as an expression. If your AWK is Gawk, you can use strftime : gawk '{ print strftime("%c", $1) }' will convert the timestamp in the first column to the current locale’s default date/time representation. You can use this to transform the content before viewing it: gawk '{ print gensub($1, strftime("%c", $1), 1) }' inputfile As Dan mentions in his answer, the Gnu date(1) command can be give...

Converting A Mongo Stored Date Back Into Milliseconds Since Unix Epoch When Loaded?

Answer : You can add the numerical milliseconds version of timestamp as a virtual attribute on the schema: schema.virtual('timestamp_ms').get(function() { return this.timestamp.getTime(); }); Then you can enable the virtual field's inclusion in toObject calls on model instances via an option on your schema: var schema = new Schema({ timestamp: Date }, { toObject: { getters: true } }); var schema = new Schema({ timestamp: {type:Number, default: new Date().getTime()} }); Hope this will solve your issue. As a best practice, I would say: keep your data the type it deserves . Anyway, if your client needs to treat with numbers, you can simply pass the date as milliseconds to the client, and still work with Date objects in Node. Just call timestamp.getTime() and ta-da, you have your unix timestamp ready for the client.

Couldn't Translate Date To Spanish With Locale("es_ES")

Answer : "es_ES" is a language + country. You must specify each part separately. The constructors for Locale are: Locale(String language) Construct a locale from a language code. Locale(String language, String country) Construct a locale from language, country. Locale(String language, String country, String variant) Construct a locale from language, country, variant. You want new Locale("es", "ES"); to get the Locale that goes with es_ES. However, it would be better to use Locale.forLanguageTag("es-ES") , using the well-formed IETF BCP 47 language tag es-ES (with - instead of _ ), since that method can return a cached Locale , instead of always creating a new one. tl;dr String output = ZonedDateTime.now ( ZoneId.of ( "Europe/Madrid" ) ) .format ( DateTimeFormatter.ofLocalizedDate ( FormatStyle.FULL ) .withLocale ( new Locale ( "es" , "ES" ) ) ) ; martes 12 de jul...

Can I Use An HTML Input Type "date" To Collect Only A Year?

Answer : No you can not but you may want to use input type number as a workaround. Look at the following example: <input type="number" min="1900" max="2099" step="1" value="2016" /> No, you can't, it doesn't support only year, so to do that you need a script, like jQuery or the webshim link you have, which shows year only. If jQuery would be an option, here is one, borrowed from Sibu: Javascript $(function() { $( "#datepicker" ).datepicker({dateFormat: 'yy'}); });​ CSS .ui-datepicker-calendar { display: none; } Src: https://stackoverflow.com/a/13528855/2827823 Src fiddle: http://jsfiddle.net/vW8zc/ Here is an updated fiddle, without the month and prev/next buttons If bootstrap is an option, check this link, they have a layout how you want. There is input type month in HTML5 which allows to select month and year. Month selector works with autocomplete. Check the examp...

Converting A String To A Date In JavaScript

Answer : The best string format for string parsing is the date ISO format together with the JavaScript Date object constructor. Examples of ISO format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS . But wait! Just using the "ISO format" doesn't work reliably by itself. String are sometimes parsed as UTC and sometimes as localtime (based on browser vendor and version). The best practice should always be to store dates as UTC and make computations as UTC. To parse a date as UTC, append a Z - e.g.: new Date('2011-04-11T10:20:30Z') . To display a date in UTC, use .toUTCString() , to display a date in user's local time, use .toString() . More info on MDN | Date and this answer. For old Internet Explorer compatibility (IE versions less than 9 do not support ISO format in Date constructor), you should split datetime string representation to it's parts and then you can use constructor using datetime parts, e.g.: new Date('2011', '04' - 1, '11'...