how do i get the current hebrew date

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

how do i get the current hebrew date

Tal Salman
how do i get the current hebrew date and parse it to: year, month, week, day

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/a7076a44-dad9-47fd-a0b0-af7dc1ae39fa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: how do i get the current hebrew date

Brent Royal-Gordon
I’m not going to write your code for you, but I’ll outline some things that will help:

The Cocoa date system consists of four major classes:

• NSDate represents an instant in time, without any sort of information about how a person would identify that instant. You can compare two NSDates to see which is earlier, or get the number of seconds between two dates, or calculate a date a certain number of seconds earlier or later. But an NSDate does not have a year, month, day, hour, minute, second, time zone, calendar system, or anything of the sort. It’s just an abstract moment in time, the way “now” or “twenty minutes from now” or “when the Berlin Wall fell" is the same time whether you’re using the Gregorian, Hebrew, Islamic, Japanese, or French revolutionary calendar.

• NSDateComponents represents a date and time as humans understand it: something with years, months, etc. You can manipulate all the individual fields, but you can do so in ways that aren’t really valid (like setting the seconds field to 90), and you can’t really compare two NSDateComponents.

• NSCalendar represents a calendar system. It can convert an NSDate into the corresponding NSDateComponents, or vice versa, bridging the gap between the two worlds. By passing a different calendar identifier when you create your NSCalendar, you can get a different system; NSCalendarIdentifierHebrew is the Hebrew calendar, and depending on what you’re trying to do, you might be interested in NSCalendarIdentifierGregorian as well.

• NSDateFormatter uses all of these classes together, plus NSLocale, to convert between NSDates and Strings. You can go in either direction, and use NSDateFormatter’s options to control almost everything about how the conversion is done, including changing the calendar system.

I’m not entirely clear on what you’re trying to do, but here are a few possibilities:

• If you’re trying to take the current date and output it as a string in the Hebrew calendar, you’ll get an NSDate representing the current moment with NSDate(), then configure an NSDateFormatter with a Hebrew NSCalendar and whatever other options you want, and convert your NSDate to a String.

• If you’re trying to take the current date and break it down into Int year/month/day/etc. so you can do something else with them, you’ll get the current date with NSDate(), then get a Hebrew NSCalendar and instruct it to convert that date to an NSDateComponents, specifying the fields you’re interested in. The resulting NSDateComponents object will contain all the information you need to do whatever it is you’re trying to do.

• If you’re converting between user-provided Gregorian dates and Hebrew dates, you’ll do one of the above in reverse to get an NSDate, then do it forward again to get the NSDateComponents in the other calendar system.

Hope this helps,


Brent Royal-Gordon
Sent from Mailbox


On Sun, Jan 25, 2015 at 5:08 AM, Tal Salman <[hidden email]> wrote:

how do i get the current hebrew date and parse it to: year, month, week, day

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/a7076a44-dad9-47fd-a0b0-af7dc1ae39fa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/1422194296619.5cdfac1a%40Nodemailer.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: how do i get the current hebrew date

Tal Salman
In reply to this post by Tal Salman
thanks for your lovely answer.
i am sure it will help me.
i will read it carefully and try to do it.
thanks


On Sunday, January 25, 2015 at 3:08:44 PM UTC+2, Tal Salman wrote:
how do i get the current hebrew date and parse it to: year, month, week, day

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/3fb76983-b795-4ca1-9985-2a1ce4d22a87%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: how do i get the current hebrew date

Tal Salman
In reply to this post by Tal Salman
ok i read all about what you mention.
but i still don't know how to convert this code from objective c to swift.
i think this code will help me get the current hebrew date:

There's a handy class called NSCalendar. You create one like this:

NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendar * hebrew = [[NSCalendar alloc] initWithCalendarIdentifier:NSHebrewCalendar];

Once you've got the calendar objects, you can use them to convert a date around to various representations:

NSDate * date = [NSDate date];
NSDateComponents * components = [gregorian components:NSUIntegerMax fromDate:date];
NSDate * hebrewDate = [hebrew dateFromComponents:components];

NSLog(@"date: %@", date);
NSLog(@"hebrew: %@", hebrewDate);





On Sunday, January 25, 2015 at 3:08:44 PM UTC+2, Tal Salman wrote:
how do i get the current hebrew date and parse it to: year, month, week, day

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/fda52779-93e7-4b2c-ad8a-f987da84a842%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: how do i get the current hebrew date

Marco S Hyman
> but i still don't know how to convert this code from objective c to swift.

Cut and paste this into a playground.  It is not necessarily a good way to
do the job but is as close to a line by line conversion as I could get.


   // NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
let gregorian = NSCalendar(calendarIdentifier: NSGregorianCalendar)

   // NSCalendar * hebrew = [[NSCalendar alloc] initWithCalendarIdentifier:NSHebrewCalendar];
let hebrew = NSCalendar(calendarIdentifier: NSHebrewCalendar)

   // NSDate * date = [NSDate date];
let date = NSDate()
let components = gregorian!.components(NSCalendarUnit(rawValue: UInt.max), fromDate: date)

   // NSDateComponents * components = [gregorian components:NSUIntegerMax fromDate:date];

   // NSDate * hebrewDate = [hebrew dateFromComponents:components];
let hebrewDate = hebrew!.dateFromComponents(components)

   // NSLog(@"date: %@", date);
   // NSLog(@"hebrew: %@", hebrewDate);

date // "Mar 2, 2015, 11:29 AM"
hebrewDate // "Nov 16, 1747, 11:29 AM"


If you are having troubles may I suggest you spend some more time with the
interoperability section of the Using Swift with Cocoa and Objective-C
doc.

Marc

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/AF1BC339-18A3-4140-B4B0-94119697A420%40snafu.org.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: how do i get the current hebrew date

Tal Salman
In reply to this post by Tal Salman
First, you are great and so helped me to understand to switch between objective c and swift from your rows.
you are right, the code i gave you not do the job i want.
so i have found how to do it:

let hebrew = NSCalendar(calendarIdentifier: NSHebrewCalendar)

    let date = NSDate()

    let formatter = NSDateFormatter()

    formatter.dateStyle = NSDateFormatterStyle.LongStyle

    formatter.timeStyle = NSDateFormatterStyle.ShortStyle

    formatter.calendar = hebrew

    println(formatter.stringFromDate(date))

this is the result i need:

12 Adar 5775 at 10:04 AM"

thanks man, very helped me




On Sunday, January 25, 2015 at 3:08:44 PM UTC+2, Tal Salman wrote:
how do i get the current hebrew date and parse it to: year, month, week, day

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/8c0ac1be-bf8b-4033-9388-32c6bcbff86a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.