Converting dates in msg file to match outlook local dates muddle - time

I am taking the MSG file and changing the date to match the local date in outlook. (same Sent date when you open the message in outlook).
But the times seem to be off 1 hour or many hours. I've dumped out the times to investigate further:-
I'm using Aspose to open the msg file.
c#
1.msg Shows: 05/01/2011 00:46 in outlook 2016. GMT
05/01/2011 00:46:07, Kind = Utc
TimeZone offset = -08:00:00
Actual UTC Time is 05/01/2011 00:46:07
Message date to universal time + timezoneoffset: 04/01/2011 16:46:07
timezoneoffset: -08:00:00
calculated universal msg date: 04/01/2011 16:46:07
output 04/01/2011 08:46 AM
2.msg Shows 20/06/2016 16:25 in outlook 2016. GMT
20/06/2016 16:25:23, Kind = Local
TimeZone offset = 02:00:00
Actual UTC Time is 20/06/2016 15:25:23
Message date to universal time + timezoneoffset: 20/06/2016 17:25:23
timezoneoffset: 02:00:00
calculated universal msg date: 20/06/2016 17:25:23
output 06/20/2016 07:25 PM
any ideas on how to correct this to show same as outlook? I see some times are UTC and some are Local, is there a solution anyone know of?
MailMessage msg = MailMessage.Load(inFile);
MemoryStream ms = new MemoryStream();
Console.WriteLine(msg.Date.ToString() + ", Kind = " + msg.Date.Kind);
Console.WriteLine("TimeZone offset = " + msg.TimeZoneOffset);
Console.WriteLine("Actual UTC Time is " + msg.Date.ToUniversalTime().ToString());
Console.WriteLine("Message date to universal time + timezoneoffset: " + (msg.Date.ToUniversalTime() +
msg.TimeZoneOffset).ToString());
Console.WriteLine("timezoneoffset: " + msg.TimeZoneOffset.ToString());
// do calculation.
TimeZone localZone = TimeZone.CurrentTimeZone;
TimeSpan ts = localZone.GetUtcOffset(msg.Date);
msg.Date = msg.Date + msg.TimeZoneOffset;
Console.WriteLine("calculated universal msg date: " +msg.Date.ToUniversalTime().ToString());
if (msg.Date.Second >= 30) // for rounding up to match outlook
{
// ... Days, hours, minutes, seconds, milliseconds.
TimeSpan span = new TimeSpan(0, 0, 0, 31, 0);
msg.Date = msg.Date + span;
}
MhtMessageFormatter mhtlFormat = new MhtMessageFormatter();
mhtlFormat.DateTimeFormat = "ddd MM/dd/yyyy hh:mm tt";
mhtlFormat.Format(msg);
MhtSaveOptions mhtSaveOptions = new MhtSaveOptions();
mhtSaveOptions.MhtFormatOptions = MhtFormatOptions.None;
mhtSaveOptions.MhtFormatOptions = mhtSaveOptions.MhtFormatOptions | MhtFormatOptions.HideExtraPrintHeader;
Thanks,
Lee.

As assisted on your thread in Aspose.Email forum, we believe your query has already met an answer. If you still find any issue or have confusion, you can inquire further on respective thread.
I work with Aspose as Developer Evangelist.

Related

How to get the same output of departed Date.parse() in groovy?

I have an application that runs the old version of the spring application. The application has the function to create date objects using Date.parse as follows
Date getCstTimeZoneDateNow() {
String dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
def zonedDateString = new Date().format(dateFormat, TimeZone.getTimeZone('CST'))
Date date = Date.parse(dateFormat, zonedDateString)
return date // Tue Oct 18 20:36:12 EDT 2022 (in Date)
}
However, the code above is deprecated. I need to produce the same result.
I read other posts and it seems like Calender or SimpleDateFormatter is preferred.
And I thought SimpleDateFormatter has more capabilities.
This post helped me understand more about what is going on in the following code
SimpleDateFormat parse loses timezone
Date getCstTimeZoneDateNow() {
Date now = new Date()
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
SimpleDateFormat sdf = new SimpleDateFormat()
sdf.setTimeZone(TimeZone.getTimeZone('CST'))
// cstDateTime prints times in cst
String cstDateTime = sdf.format(now) // 2022-10-18T20:36:12.088Z (in String)
// JVM current time
Date date = sdf.parse(cstDateTime) // Tue Oct 18 21:36:12 EDT 2022 (in Date)
return date
}
Here my goal is to return the date object that is in the format of Tue Oct 18 20:36:12 EDT 2022
The format is good. However, like the post says, when I do sdf.parse(), it prints in JVM time.
This means, the format is good but the time zone is off.
How can I get the exact same result as before?
It does not have to use SimpleDateFormatter. Could be anything.
Thank you so much for reading and for your time.
Perhaps the important thing is, that the Date is always neutral to the timezone. Given example shows what is to be expected to work from the Java specs:
def format = new SimpleDateFormat()
format.setTimeZone(TimeZone.getTimeZone("CST"))
println new Date()
def date = format.parse(format.format(new Date()))
printf "parsed to %s%n", date
printf "formatted to %s (%s)%n", format.format(date), format.getTimeZone().getDisplayName()
In the output, notice when using the Format and when the toString(), a different time is shown accordingly, which is perfectly fine, since first we format and then parse again in the same format, thus the same time-zone. Later, we use the Date.toString() to output the date, this time using the system default time-zone which is always used when Date.toString() is called. In the output, the time-zone shift is reflected:
Thu Oct 20 09:22:58 EDT 2022
parsed to Thu Oct 20 09:22:00 EDT 2022
formatted to 10/20/22 8:22 AM (Central Standard Time)

What is this time format? (10 digits, 5 decimals)

So a website that I'm using has a websocket and they provide the broadcast time in the following manner:
"broadcasted_at":1574325570.71308
What is this time format and how do they generate it?
Unix epoch time ... the number of seconds that have elapsed since the Unix epoch, that is the time 00:00:00 UTC on 1 January 1970
now : 1574327074 : Thu Nov 21 03:04:34 2019
start of day : 1574316000 : Thu Nov 21 00:00:00 2019
1574325570 : 1574325570 : Thu Nov 21 02:39:30 2019
convert online : https://www.epochconverter.com/
... or download code (to build) to have command line program to perform the conversion https://github.com/darrenjs/c_dev_utils
I'm guessing the fractional part is the number of microseconds within the current second.
… and how do they generate it?
I don’t know, of course, what language or libraries your website is using. So this is just an example. To generate a value like 1574325570.71308 in Java:
Instant now = Instant.now();
double epochSeconds = now.getEpochSecond()
+ (double) now.getNano() / (double) TimeUnit.SECONDS.toNanos(1);
String result = String.format(Locale.ROOT, "%f", epochSeconds);
System.out.println("result: " + result);
When I ran this snippet just now (2019-12-15T11:18:01.562699Z), the output was:
result: 1576408681.562699
If you want exactly 5 decimals always another way is to use a DateTimeFormatter:
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendValue(ChronoField.INSTANT_SECONDS)
.appendPattern(".SSSSS")
.toFormatter();
String result = formatter.format(now);
result: 1576408681.56269

How to convert UTC to EST by taking care of daylight savings

I am pulling the following output from a website using an API call:
out_string += "Incident Created :" + nextOncall_result['incident']['created_at'].to_s + "\n"
"created_at" gives the time when an incident was created. The website is in UTC, and I want to convert it to EST by taking care to daylight savings.
Please help me. Please change the code if possible.
In rails you can do:
nextOncall_result['incident']['created_at'].in_time_zone('EST')
UPD
require 'tzinfo'
offset = TZInfo::Timezone.get('EST').current_period.offset.utc_total_offset
time_with_dst_in_utc = nextOncall_result['incident']['created_at'] + offset
res = time_with_dst_in_utc.strftime('%d/%m/%Y %m:%S')

(Swift) Using NSDataFormatter with a medium style string to get date

I seem to be stuck here and I have been wasting way too much tome on this.
What I have is a string that is in the RFC 1123 format that I would like to get a date out of, but not matter what I do, I get a nil result;
let dateFormat = NSDateFormatter();
dateFormat.dateStyle = .MediumStyle;
dateFormat.dateFormat = "EEE',' dd MMM yyyy HH':'mm':'ss z";
dateFormat.locale = NSLocale.systemLocale();
dateFormat.timeZone = NSTimeZone(abbreviation:"GMT");
var currentDate = dateFormat.dateFromString("Sun, 28 Jun 2015 04:30:54 GMT");
I am not sure what I am missing, if I changed the MMM to MM and make Jun 06, then it works. It seems to be only this instance. I have tried moving the order of how dateFormat gets created, and still I get no results. Any help on this matter would greatly be appreciated
I think you have confused the formatter. You don't need to set anything except the format string, because the formatter's job is to learn those other settings from the string it reads.
let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "EEE',' dd MMM yyyy HH':'mm':'ss z"
var currentDate = dateFormat.dateFromString("Sun, 28 Jun 2015 04:30:54 GMT")
// "Jun 27, 2015, 11:30 PM"
If you do as above, it will return an NSDate? from the date string you provided.

How do I get the Date & Time (VBS)

How do I get the current date and time using VBS (for Windows. I'm not looking for VBScript for ASP/ASPX or webpages).
Here's various date and time information you can pull in vbscript running under Windows Script Host (WSH):
Now = 2/29/2016 1:02:03 PM
Date = 2/29/2016
Time = 1:02:03 PM
Timer = 78826.31 ' seconds since midnight
FormatDateTime(Now) = 2/29/2016 1:02:03 PM
FormatDateTime(Now, vbGeneralDate) = 2/29/2016 1:02:03 PM
FormatDateTime(Now, vbLongDate) = Monday, February 29, 2016
FormatDateTime(Now, vbShortDate) = 2/29/2016
FormatDateTime(Now, vbLongTime) = 1:02:03 PM
FormatDateTime(Now, vbShortTime) = 13:02
Year(Now) = 2016
Month(Now) = 2
Day(Now) = 29
Hour(Now) = 13
Minute(Now) = 2
Second(Now) = 3
Year(Date) = 2016
Month(Date) = 2
Day(Date) = 29
Hour(Time) = 13
Minute(Time) = 2
Second(Time) = 3
Function LPad (str, pad, length)
LPad = String(length - Len(str), pad) & str
End Function
LPad(Month(Date), "0", 2) = 02
LPad(Day(Date), "0", 2) = 29
LPad(Hour(Time), "0", 2) = 13
LPad(Minute(Time), "0", 2) = 02
LPad(Second(Time), "0", 2) = 03
Weekday(Now) = 2
WeekdayName(Weekday(Now), True) = Mon
WeekdayName(Weekday(Now), False) = Monday
WeekdayName(Weekday(Now)) = Monday
MonthName(Month(Now), True) = Feb
MonthName(Month(Now), False) = February
MonthName(Month(Now)) = February
Set os = GetObject("winmgmts:root\cimv2:Win32_OperatingSystem=#")
os.LocalDateTime = 20131204215346.562000-300
Left(os.LocalDateTime, 4) = 2013 ' year
Mid(os.LocalDateTime, 5, 2) = 12 ' month
Mid(os.LocalDateTime, 7, 2) = 04 ' day
Mid(os.LocalDateTime, 9, 2) = 21 ' hour
Mid(os.LocalDateTime, 11, 2) = 53 ' minute
Mid(os.LocalDateTime, 13, 2) = 46 ' second
Dim wmi : Set wmi = GetObject("winmgmts:root\cimv2")
Set timeZones = wmi.ExecQuery("SELECT Bias, Caption FROM Win32_TimeZone")
For Each tz In timeZones
tz.Bias = -300
tz.Caption = (UTC-05:00) Eastern Time (US & Canada)
Next
Source
To expound on Numenor's answer you can do something like, Format(Now(),"HH:mm:ss")
using these custom date/time formating options
For everyone who is tempted to downvote this answer please be aware that the question was originally tagged VB and vbscript hence my answer, the VB tag was edited out leaving only the vbscript tag. The OP accepted this answer which I take to mean that it gave him the information that he needed.
For VBScript use FormatDateTime, which has 5 numerical arguments to give you one of 5 predefined formats. Its not great.
FormatDateTime(now, 4)
08:12
This is an old question but alot of the answers in here use VB or VBA. The tag says vbscript (which is how I got here).
The answers here got kind of muddled since VB is super broad where you can have so many applications of it. My answer is solely on vbscript and accomplishes my case of formatting in YYYYMMDD in vbscript
Sharing what I've learned:
There are all the DateTime functions in vbscript defined here so you can mix-n-match to get the result that you want
What I needed was to get the current date and format it in YYYYMMDD to do that I just needed to concat DatePart like so for the current Date: date = DatePart("yyyy",Date) & DatePart("m",Date) & DatePart("d",Date)
That's all, I hope this helps someone.
Show time in form 24 hours
Right("0" & hour(now),2) & ":" & Right("0" & minute(now),2) = 01:35
Right("0" & hour(now),2) = 01
Right("0" & minute(now),2) = 35
nowreturns the current date and time
There are also separate Time() and Date() functions.

Resources