How do you parse a date and time string with offset using NodaTime? - nodatime

I am trying to learn how to use NodaTime within my application, but cannot find very many examples of how to do certain things with this library.
Given:
date/time text of "2012/08/30 17:45:00"
the format string is "yyyy/MM/dd HH:mm:ss"
the date/time offset from UTC is -5
How do I parse this with NodaTime to get an
OffsetDateTime?
Instant?

Using pure NodaTime code, there is not currently a direct parser for an OffsetDateTime. See the documented limitations. However, you can construct one by parsing a LocalDateTime and an Offset separately:
var ldt = LocalDateTimePattern.CreateWithInvariantCulture("yyyy/MM/dd HH:mm:ss")
.Parse("2012/08/30 17:45:00")
.Value;
var o = OffsetPattern.GeneralInvariantPattern
.Parse("-05")
.Value;
var odt = new OffsetDateTime(ldt, o);
There is a similar parser for Instant, but it requires a UTC time - not an offset.
You could also just use the text parsing for DateTimeOffset in the BCL, and then do:
var odt = OffsetDateTime.FromDateTimeOffset(dto);
Either way, once you have an OffsetDateTime, it is convertible to an Instant:
var instant = odt.ToInstant();

Related

How can I store String type DateTime string to ISO DateTime Format in MongoDB in NodeJS?

I am receiving a string format DateTime Strings data from the android app.
according to the previous developer's note, it's RFC 3339 format string.
I am stuck in converting this string into MongoDB's Date format in Node.js
in detail edit documents, formats are like below.
[ current string what I receive -> Date format in MongoDB, which I wanna store]
[ "2020-09-14 08:18:56", -> ISODate("2020-09-14T08:42:41.000Z") ]
Is there any way to save those strings into MongoDB on NodeJS programmatically?
RFC 3339 is a profile (a set of standards derived) of ISO 6081 (see these SO posts: [1], [2]), so you can just pass the string to Date's constructor and get a valid js Date:
var raw = "2020-09-14 08:18:56";
var date = new Date(raw);
console.log(date);

how to compare dates using relational Operators in dart?

I'm working on a problem which requires to compare the date with other if the selected date is less than given then print Hello.
The date is present In String Format like given in Examaple
Ex:
if('2020-1-13'<'2021-1-5')
{
print(hello);
}
You should try this it should work
````
var date1 = DateTime.parse('2020-01-13').millisecondsSinceEpoch.toInt();
var date2 = DateTime.parse('2021-01-15').millisecondsSinceEpoch.toInt();
if(date1 < date2){
print('true');
}else{
print('false');
}
````
Instead of doing that just use the date's isBefore or isAfter operator
It would look something like the following:
final now = DateTime.now();
final yesterday = DateTime.now().subtract(Duration(days: 1));
print(now.isAfter(yesterday)); //true
Since your dates are in STring format, it you would have to use the DateTime.parse() method to construct your date objects, and then use the operators talked about above
package:basics provides DateTime extensions that can easily compare DateTime objects.
(Disclosure: I contributed those particular extensions.)

Convert Excel Date string to Unix time in Node.js

I use node-xlsx for parsing Excel file in Node.js but Date cells have strange view before parsing:
in Excel: 01.03.2016 07:44:04
in Node.js before parsing: 42430.32226851852
How I can convert this string 42430.32226851852 to Unix-time format?
Update:
Write myself solution that work for me:
var xlsxDate = '42430.32226851852'
var splitDate=xlsxDate.split('.');
var unixTime=new Date(1900, 0, splitDate[0]-1, 0, 0, Math.round(splitDate[1]/1157410)).getTime()/1000
console.log(unixTime)
Where 1157410 is ~1 second
i use this code to convert Unix-time format:
date = new Date(( parseInt(excelDate) - (25567 + 2))*86400*1000)
unix = date.getTime()
Another work around is that when you save it to your excel document you save it as a string and not a Date object.
Then you can read it as a string and just wrap it in a new Date()

Converting UTC Date to GMT in Groovy / Java

I am working with SoupUI a i need to adjust a Date/time (UTC) that i get back in a response to a GMT Date/time. The date that i get back in the respone looks as followes:
2012-11-09T00:00:00+01:00
I would like to convert this to
2012-11-08T23:00:00Z
Unfortunatly i lack Java skils and therefore also Groovy skils to be able to do this on my own. i did a lot o searches on date convertions but until now i was still unable to find what i was looking for. i will keep searching. if i do manage to get the solution then i will post it here.
Assuming there isn't a colon in the timezone portion, I believe this should work:
// Your input String (with no colons in the timezone portion)
String original = '2012-11-09T00:00:00+0100'
// The format to read this input String
def inFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssZ" )
// The format we want to output
def outFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss'Z'" )
// Set the timezone for the output
outFormat.timeZone = java.util.TimeZone.getTimeZone( 'GMT' )
// Then parse the original String, and format the resultant
// Date back into a new String
String result = outFormat.format( inFormat.parse( original ) )
// Check it's what we wanted
assert result == '2012-11-08T23:00:00Z'
If there is a colon in the TimeZone, you'll need Java 7 for this task (or maybe a date handling framework like JodaTime), and you can change the first two lines to:
// Your input String
String original = '2012-11-09T00:00:00+01:00'
// The format to read this input String (using the X
// placeholder for ISO time difference)
def inFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssX" )

DateTime Hex Format

I'm getting a hex string when converting a SPListItem's date.
eg. "0x01c9d2be|0x809a8800"
This question answers it.
However, what's the proper way to convert it, what format is the DateTime object in?
What if I want to set the datetime? Would I have to convert it into hex format and assign it as a hex string?
There has to be a better and ideal way to extract modify the DateTime object.
Any ideas?
Thanks.
I'm able to extract the date and time. That's fine.
So when you are modifying the DateTime of a ListItem, you just simply assign it as a DateTime format and it will interpret it correctly; no need to generate the hex that it returns.
A DateTime value in .NET can also be represented as a 64-bit value. The strange string format on DateTime fields on SharePoint list items is a variant of such a 64-bit value encoded as two 32-bit hex values. I have found no documentation from Microsoft on this format. But by trial and error I found the following conversion method:
string[] words = strVal.Split('|');
int high = int.Parse(words[0].Substring(2), System.Globalization.NumberStyles.HexNumber);
uint low = uint.Parse(words[1].Substring(2), System.Globalization.NumberStyles.HexNumber);
long ticks = high;
ticks = ticks << 32;
ticks += low;
DateTime t = new DateTime(ticks);
t = t.AddYears(1600);
where t holds the result.
Another solution can be found here. It has something to do how Office 2007 stores dates..
SPListItem.Properties DateTime Fields are in weird Hex format

Resources