Current timestamp in ISO 8601 UTC time (ie: 2013-11-03T00:45:54+02:00) - date

Can someone please help me how to format timestamp in ISO 8601 UTC time?
I want the date to be formatted like this 2020-10-03T00:45:54+02:00
I have tried this in App Script.
Utilities.formatDate(new Date(), "UTC", "yyyy-MM-dd'T'HH:mm:ssZZZ");
It is getting this output 2020-10-03T08:50:18+0000
I want the timezone to be formatted like this TwoDigitHours : Minutes

Use Date.toISOString() for UTC ISO8601 string:
/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/
console.log(new Date().toISOString())
<!-- https://meta.stackoverflow.com/a/375985/ --> <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
Utilities.formatDate accepts 3 arguments:
Date object
Timezone
SimpleDateFormat string
For, ISO8601 timestring use X instead of Z
For a specific timezone, use that timezone(GMT+2) as second argument instead of UTC
Utilities.formatDate(new Date(),"GMT+2" , "yyyy-MM-dd'T'HH:mm:ssXXX");
Alternatively, Use Session.getScriptTimeZone() or Spreadsheet.getSpreadsheetTimeZone() instead for the second argument:
Utilities.formatDate(new Date(),Session.getScriptTimeZone(), "yyyy-MM-dd'T'HH:mm:ssXXX");
//Or
Utilities.formatDate(new Date(),Spreadsheet.getSpreadsheetTimeZone(), "yyyy-MM-dd'T'HH:mm:ssXXX");

Related

talend format yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss

I am trying to change the date format in txmlmap component but its not working
i want change date format
from yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss
expected output:- yyyy-mm-dd HH:mm:ss
You can parse your string to a date using your source pattern and then format that date to a string using your target pattern:
TalendDate.formatDate("yyyy-mm-dd HH:mm:ss", TalendDate.parseDate("yyyy-MM-dd'T'HH:mm:ss.SSSz", myDateString))
In almost all coding languages format is text, while date is a double. That means you must first make a date of the first expression, before setting the new format of that date. But in Your case the 'T' is some kind of special format that need to be replaced with a blanck space. I have no idea about what it would look like in talend but in VB it would look like this:
' from yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss
DateTxt = "2022-12-01'T'22:45:10"
DateTxt = Replace(DateTxt, "'T'", " ")
MyDate = CDate(DateTxt)
MsgBox Format(MyDate, "yyyy-mm-dd HH:mm:ss")

How to convert a date to a specific timezone when user is in a different timezone?

I have a problem in handling dates in Swift in different timezones. I'm getting an Italian date from my server and I'm trying to convert it into a Date through the following function:
func stringToDateTime(dateString: String) -> Date? {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
formatter.locale = Locale(identifier: "it_IT")
formatter.timeZone = TimeZone(identifier: "Europe/Rome")
return formatter.date(from: dateString)
}
However if the date is "2022-11-22 10:00:00", for example, and I'm in London or New York the function returns:
▿ 2022-11-22 09:00:00 +0000
- timeIntervalSinceReferenceDate : 690800400.0
Why is that so? I specified the timezone in the formatter so it should return me 10 o'clock. What am I doing wrong?
The date and time seems correct to me.
The date and time which was passed "2022-11-22 10:00:00" is in Italian time, which is GMT+1, or with other words, 1 hour ahead of UTC.
The date and time which was printed to the console is in UTC, or GMT+0, and is indicated by '+0000'.
2022-11-22 09:00:00 +0000
If we were to put this date into a converter for UTC to Italian time, we would get the initial value - "2022-11-22 10:00:00".
When you print a Date in the console directly, it automatically uses UTC as the time zone. If you'd like to print a string representation of your Date object to the console in your locale, use this method:
Generates a locale-aware string representation of a date using the default date format style.
func formatted() -> String
from Apple documentation: https://developer.apple.com/documentation/swift/sequence/formatted(_:)

Convert the String Monday 5 October to date using Google Apps Script

I need to convert the date string of type WEEKDAY DATE MONTHNAME, Example: from "Monday 5 October" to date object.
I have tried with
Utilities.formatDate(new Date("Monday 5 October"), "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'")
How do I convert it, I am ok using V8 apps script engine
The Date constructor accepts timestamp strings formatted according to IETF-compliant RFC 2822 timestamps and ISO8601.
There are many ways to convert your string to date, but probably one of the simplest is appending the current year to your string, using getFullYear():
const source = "Monday 5 October";
const date = new Date(`${source} ${new Date().getFullYear()}`);
Reference:
Date() constructor
IETF-compliant RFC 2822 timestamps

how to convert a String to a Date without changing the Format

I'm using Xcode 11.4.1
In my Project I got a String with some data which I need to split up and save the Information in some variables.
now I face a problem if I try this
let dateString = "12-04-2020"
let dateFormatIn = DateFormatter()
dateFormatIn.dateFormat = "dd-MM-yyyy"
let saveDate: Date = dateFormatIn.date(from: dateString)!
print("The date is: \(saveDate)")
the result which I expected is "The date is: 12-04-2019" but what I got is "The date is: 2020-04-11 15:00:00 +0000"
what do I miss, what is this printed Date? it's not the current date ans also not the String!
I need the Date in the same format as I got it in the String.
A Date is a point in time, it doesn't have a format nor a time zone.
print shows the description – a string representation – of the date in the UTC time zone.
Your time zone is obviously UTC +0900, 2020-04-12 00:00:00 +0900 and 2020-04-11 15:00:00 +0000 is the same point in time.
If you want to create the date string independent of the time zone add the line
dateFormatIn.timeZone = TimeZone(secondsFromGMT: 0)
let saveDate: Date = dateFormatIn.date(from: dateString)!
The ": Date" looks like a data type declaration. You're saying, "create a variable called saveDate of type Date, and initialize it from the output of dateFormatIn.date(from: dateString)".
So you're not printing the formatted output. You're converting the formatted output to a new Date object, and then printing that.
Try:
let saveDate = dateFormatIn.date(from: dateString)!
This should save whatever dateFormatIn.date(from: dateString) returns in saveDate.

Swift: Get correct time zone from Date Picker?

I am trying to get the correct time zone from the date picker in swift using time formatter, it's not working. I'm getting UTC, not EST.
1) If I print dateFormatter.stringFromDate(datePicker) I get EST, but
2) I don't need a string, I need an NSDate in EST so
3) I can use it to get the timeIntervalSinceDate(NSDate) in EST.
My trick of trying to take it from string back to NSDate as seen below didn't work. It's still in UTC and the time interval since date is not right.
dateFormatter.locale = NSLocale.currentLocale()
dateFormatter.timeZone = NSTimeZone.localTimeZone()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date: NSDate = dateFormatter.dateFromString(dateFormatter.stringFromDate(datePicker))!
print(date)
print(date.timeIntervalSinceDate(datePicker))
The above answer is totally wrong. Date picker report the date in system locale anytime, so, if the datePicker shows an 08:00 Time and you are GMT+2, the property date of the picker will be 06:00.
So for have the absolute value of the datePicker you have to pass to him the UTC time zone in view did load with:
datePicker.timeZone = TimeZone.init(identifier: "UTC")
Now, the date property of the picker will be the expected and choosen one.
You cannot "get a time zone" from a date picker. You can just get a date. The date will be independent on the current time zone of the device.
Perhaps you think you have a different date, but actually, there is no such thing as a "UTC date" or "EST date". Instead, there is only one date, and you use date formatters to display them for various time zones.
Note that there is quite a bit of redundancy in your code. The default locale and time zone of a date formatter are already the same values that you set. Also, when you have a method that returns a NSDate you do not have annotate the constant with : NSDate, making your code more verbose and cluttered.
Note that if you print a date the console will always show UTC. e.g.
let date = NSDate() // Nov 10, 9:44 PM
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd hh:mm a"
let dateString = dateFormatter.stringFromDate(date) // "2015-11-10 09:44 PM"
print(date) // "2015-11-10 20:44:54 +0000\n"
To set the TimeZone of a DatePicker to UTC use:
datePicker.timeZone = TimeZone.init(identifier: "UTC")
Notice the camelcase notation of "timeZone".
Unfortunately I don't have enough credit to comment on the last post, which has it almost right, so I had to create a new answer.
A little trivia: TimeZone has been around since iOS 2.0 as is stated here:
https://developer.apple.com/documentation/uikit/uidatepicker/1615976-timezone
Those who are trying to find a solution and are not able to wrap their head around the exact issue. Here is what something I tried:
Use time.addTimeInterval(-14400) function, where -14400 is the 4 hours difference. So if you want UTC to EST do this.
Something like this:-
var time = Date() // assuming you have this in UTC
time.addTimeInterval(-14400)
You can also use addingTimeInterval function which returns you the new date.
I know this is a little wonky cause we're manually doing this, but hope it helps someone.