How can I remove time from datetime string in dart - string

I have a Date time string 2021-12-12T11:11:00. I want to remove the substring T11:11:00, so that I am left with 2021-12-12.
Does anyone have any ideas? Please help me.

import 'package:intl/intl.dart'; //note --->>> import package
DateTime parsedDateTime = DateTime.parse('2021-12-12T11:11:00');
String formatDate = DateFormat("yyyy-MM-dd").format(parsedDateTime);
OUTPUT ->
2021-12-12

String _date_and_time = "2021-12-12T11:11:00";
String date = _date_and_time.split("T")[0];
print("Date is : ${date}");

Related

Dealing with trailing decimals at the end of a DateTime

I am trying to convert the string below into a datetime object using datetime.strptime, and I just can't seem to figure out .746Z at the end.
datetime_str = '2022-04-21T08:17:49.746Z'
datetime_object = datetime.strptime(datetime_str, '%Y-%m-%dT%H:%M:%S.%z')
print(datetime_object)

Change panda date format into another date format?

How do I convert this format below into the result format ?
import pandas as pd
date = pd.date_range('2022-01-01',2022-01-31', freq = 'H')
Result:
'2021-01-01T01%3A00%3A00',
What is the correct name for the result time format ? Have tried using urlilib.parse module, but it did not have T and it can take 1 date.
Thank you !
This so called url encode , so we need urllib, notice here %3A = ':'
import urllib
date.astype(str).map(urllib.parse.quote)
Out[158]:
Index(['2022-01-01%2000%3A00%3A00', '2022-01-01%2001%3A00%3A00',
....

How to insert todays date automatically?

I want to insert today's date in the following code automatically.
import shutil
shutil.copy(r"C:\Users\KUNDAN\Desktop\Backup\Cash MAR.2017 TO APR.2019.accdb",
r"F:\Cash MAR.2017 TO APR.2019 (11-09-19).accdb ")
print("DONE !!!")
First off, I'm not 100% sure of your question. If this doesn't answer it as you're expecting, please reformat your question to add clarity.
You can do this via the datetime library and string formatting. For example (using UK date format):
import shutil
from datetime import datetime as dt
today = dt.today().strftime('%d-%m-%y')
src = "C:/Users/KUNDAN/Desktop/Backup/Cash MAR.2017 TO APR.2019.accdb"
dst = "F:/Cash MAR.2017 TO APR.2019 ({today}).accdb".format(today=today)
shutil.copy(src, dst)
print("DONE !!!")
I have found these two links very useful:
string formatting
datetime formatting using strftime
import datetime
datetime_now = datetime.datetime.now()
print(datetime_now)
It will print the date and time for now and you can choose what format you like.

date-time-string to UNIX time with milliseconds

I need to convert a date/time string to UNIX timestamp including the milliseconds. As the timetuple() does not include milli or microseconds I made a short workaround. But I was wondering, is there a better/nicer way to do this?
import datetime as dt
import time
timestamp = '2018-01-19 10:00:00.019' # example of input time string
tmp = timestamp.split('.')
millisec = tmp[-1] # extracting only milli-seconds
UX_time = time.mktime(dt.datetime.strptime(tmp[0], '%Y-%m-%d %H:%M:%S').timetuple()) + float(millisec)/1e3
print(UX_time)
1516352400.019
I realize my timezone is off by one hour, so you might be getting
print(UX_time)
1516356000.019
you can try this:
timestamp = '2018-01-19 10:00:00.019'
tmp=np.datetime64(timestamp)
print(tmp.view('<i8')/1e3)
output:
1516352400.019
Also possible with your current code:
import datetime as dt
import time
timestamp = '2018-01-19 10:00:00.019' # example of input time string
ts = dt.datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f')
UX_time = time.mktime(ts.timetuple()) + ts.microsecond/1e6
print "%.3f" %UX_time

get current date and time in groovy?

What is the code to get the current date and time in groovy? I've looked around and can't find an easy way to do this. Essentially I'm looking for linux equivalent of date
I have :
import java.text.SimpleDateFormat
def call(){
def date = new Date()
sdf = new SimpleDateFormat("MM/dd/yyyy")
return sdf.format(date)
}
but I need to print time as well.
Date has the time as well, just add HH:mm:ss to the date format:
import java.text.SimpleDateFormat
def date = new Date()
def sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss")
println sdf.format(date)
In case you are using JRE 8+ you can use LocalDateTime:
import java.time.LocalDateTime
def dt = LocalDateTime.now()
println dt
Date has the time part, so we only need to extract it from Date
I personally prefer the default format parameter of the Date when date and time needs to be separated instead of using the extra SimpleDateFormat
Date date = new Date()
String datePart = date.format("dd/MM/yyyy")
String timePart = date.format("HH:mm:ss")
println "datePart : " + datePart + "\ttimePart : " + timePart
A oneliner to print timestamp of your local timezone:
String.format('%tF %<tH:%<tM', java.time.LocalDateTime.now())
Output for example: 2021-12-05 13:20
Answering your question: new Date().format("MM/dd/yyyy HH:mm:ss")
#!groovy
import java.text.SimpleDateFormat
pipeline {
agent any
stages {
stage('Hello') {
steps {
script{
def date = new Date()
sdf = new SimpleDateFormat("MM/dd/yyyy")
println(sdf.format(date))
}
}
}
}
}

Resources