I have a variable recover from ajax and in the controller
dd($gethour);
"Sat Oct 26 2019 00:00:04 GMT+0200 (heure d’été d’Europe centrale)"
$from = \DateTime::createFromFormat('m-d-y H:i:s', $gethour);
dd($form);
Result:false
My problem is how to convert string to timestamp seen in the base HoursPass is a type timestamp
It would be better if you could start with a real timestamp which is long number. But otherwise you could do it this way:
$gethour = "Sat Oct 26 2019 00:00:04 GMT+0200 (heure d’été d’Europe centrale)";
$chunks = explode(' ', $gethour);
$from = new \DateTime($chunks[1] . ' ' . $chunks[2] . ' ' . $chunks[3] . ' ' . $chunks[4]);
$from->setTimezone(new DateTimeZone($chunks[5]));
echo $from->format('d-m-Y H:i:s P'); // 26-10-2019 09:00:04 +02:00
you can exam this way :
date("m-d-y H:i:s", strtotime($gethour));
this way is answered for me.
Related
I have this in c#:
var date = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
and the result is like this:
date = "Tue, 27 Dec 2022 13:30:35 GMT";
I want to have this result in pre-request of postman to pass this variable as date.
But this command doesn't give me the exact result:
var date = new Date();
//result: Tue Dec 27 2022 16:26:00 GMT+0100 (Central European Standard Time)
As I'm using this date variable for encryption, it's important to have it in the special format I have in c#.
Do you have any idea how can I have this result in postman?
To display time, you can use momentjs, that's already included in postman. The cons is it doesn't support timezone, so the code would be:
const moment = require('moment')
let datetime = moment().format("ddd, DD MMM YYYY HH:mm:ss ") + "GMT"
//Wed, 28 Dec 2022 08:08:36 GMT
Using reg expression in pre-request section
var date = new Date();
// Tue Dec 27 2022 12:10:39 GMT-0500 (Eastern Standard Time)
console.log(date);
let match = /(Sun|Mon|Tue|Wed|Thu|Fri|Sat)\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+(\d{1,2})\s+(\d{4})\s+(\d{2}|\d{1})\:(\d{2})\:(\d{2})\s([a-zA-Z]{3})/.exec(date);
// 0: "Tue Dec 27 2022 12:10:39 GMT"
// 1: "Tue"
// 2: "Dec"
// 3: "27"
// 4: "2022"
// 5: "12"
// 6: "10"
// 7: "39"
// 8: "GMT"
// newDate = "Tue, 27 Dec 2022 13:30:39 GMT";
newDate = `${match[1]}, ${match[3]} ${match[2]} ${match[4]} ${match[5]}:${match[6]}:${match[7]} ${match[8]}`
console.log(newDate);
Result in console
Tue Dec 27 2022 12:22:39 GMT-0500 (Eastern Standard Time)
Tue, 27 Dec 2022 12:22:39 GMT
Test string set in https://regex101.com/
Regular Expression
(Sun|Mon|Tue|Wed|Thu|Fri|Sat)\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+(\d{1,2})\s+(\d{4})\s+(\d{2}|\d{1})\:(\d{2})\:(\d{2})\s([a-zA-Z]{3})
In Reg Expression Visualization https://regexper.com/
I have a dataset that looks like this:
datetime count
18:28:20.602 UTC DEC 08 2016 1
20:42:32.017 UTC DEC 08 2016 5
15:33:40.691 UTC DEC 08 2016 1
17:11:54.008 UTC DEC 08 2016 3
20:28:57.861 UTC DEC 08 2016 0
.
.
.
.
The datetime column is in the string format. I'm having difficulty in converting it to a timestamp.
How do I write a Impala/Hive query so that I get the data between '18:28:00.000 UTC DEC 08 2016' to '18:33:00.000 UTC DEC 08 2016'
With Hive:
cast(from_unixtime(unix_timestamp(SHITTY_FORMAT, 'HH:mm:ss.SSS zzz MMM dd yyyy'), 'yyyy-MM-dd HH:mm:ss.SSS') as Timestamp)
...will translate your shitty String format into a UNIX timestamp, then into String standard format (in local timezone because that's the Hive convention), then into a Timestamp.
There is no easier way, unfortunately. And you may have some edge cases because of the 1h overlap in summer/winter times.
Source: the Hive documentation, of course...
With Impala (which does not support the zzz format modifier):
cast(from_unixtime(unix_timestamp(regexp_replace(SHITTY_FORMAT, ' UTC ', ' '), 'HH:mm:ss.SSS MMM dd yyyy'), 'yyyy-MM-dd HH:mm:ss.SSS') as Timestamp)
...will translate your shitty String format into a UNIX timestamp, assuming that all your inputs are in UTC, then into String standard format (in UTC timezone because that's the Impala convention), then into a Timestamp.
I have a calendar that posts the value in the following format
3 January, 2017
and when I convert it to carbon by doing
$carbon = Carbon::parse($data['due_date']);
echo $carbon;
I see 2016-01-03 20:17:00
My expected output is 2017-01-03
Try
$date = Carbon::createFromFormat('j F, Y', $data['due_date']);
Just use this 👇
echo date('Y-m-d', strtotime('3 January, 2017'));
As a beginner, creating timestamps or formatted dates ended up being a little more of a challenge than I would have expected. What are some basic examples for reference?
Ultimately you want to review the datetime documentation and become familiar with the formatting variables, but here are some examples to get you started:
import datetime
print('Timestamp: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))
print('Timestamp: {:%Y-%b-%d %H:%M:%S}'.format(datetime.datetime.now()))
print('Date now: %s' % datetime.datetime.now())
print('Date today: %s' % datetime.date.today())
today = datetime.date.today()
print("Today's date is {:%b, %d %Y}".format(today))
schedule = '{:%b, %d %Y}'.format(today) + ' - 6 PM to 10 PM Pacific'
schedule2 = '{:%B, %d %Y}'.format(today) + ' - 1 PM to 6 PM Central'
print('Maintenance: %s' % schedule)
print('Maintenance: %s' % schedule2)
The output:
Timestamp: 2014-10-18 21:31:12
Timestamp: 2014-Oct-18 21:31:12
Date now: 2014-10-18 21:31:12.318340
Date today: 2014-10-18
Today's date is Oct, 18 2014
Maintenance: Oct, 18 2014 - 6 PM to 10 PM Pacific
Maintenance: October, 18 2014 - 1 PM to 6 PM Central
Reference link: https://docs.python.org/3.4/library/datetime.html#strftime-strptime-behavior
>>> import time
>>> print(time.strftime('%a %H:%M:%S'))
Mon 06:23:14
from datetime import datetime
dt = datetime.now() # for date and time
ts = datetime.timestamp(dt) # for timestamp
print("Date and time is:", dt)
print("Timestamp is:", ts)
You might want to check string to datetime operations for formatting.
from datetime import datetime
datetime_str = '09/19/18 13:55:26'
datetime_object = datetime.strptime(datetime_str, '%m/%d/%y %H:%M:%S')
print(type(datetime_object))
print(datetime_object) # printed in default format
Output:
<class 'datetime.datetime'>
2018-09-19 13:55:26
PayPal's IPN gives dates in this format 06:52:15 Apr 12, 2014 PDT which is not a valid recognised format. Also, the timezone is specified; I can't find a ColdFusion date class that can take a timezone. So, what's the shortest way to convert the format and translate the timezone to GMT (the timezone of my server). So far I have this...
arr3LA = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dev'];
arrDateParts = ListToArray( FORM.payment_date, ' ');
arrTimeParts = ListToArray( arrDateParts[1], ':' );
arrDateParts[3] = Replace(arrDateParts[3], ',', '', 'All');
objPayPalDateTime = CreateDateTime( arrDateParts[4], ArrayFind( arr3LA, arrDateParts[2]), arrDateParts[3], arrTimeParts[1], arrTimeParts[2], arrTimeParts[3] );
strLSFriendly = DateFormat( objPayPalDateTime, 'mmmm dd, yyyy' ) & ' ' & TimeFormat( objPayPalDateTime, 'h:mm:ss tt' ) & ' ' & arrDateParts[5];
objDateTime = LSParseDateTime( strLSFriendly );
strSQLFriendlyDateTime = DateFormat( objDateTime, 'yyyy-mm-dd hh:mm:ss' );
Can you improve on that?
I'd say forget using native CFML stuff here, and leverage java:
target = "06:52:15 Apr 12, 2014 PDT";
sdf = createObject("java", "java.text.SimpleDateFormat").init("hh:mm:ss MMM dd, yyyy zzz");
result = sdf.parse(target);
writeDump([{target=target},{result=result}]);
Does that do the trick?
(NB: inspired by this question: How to parse date string to Date?)